• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 6657559791

26 Oct 2023 05:17PM UTC coverage: 28.072% (+0.002%) from 28.07%
6657559791

push

github

mcallegari
ui: add helper to load more custom styles from file

40 of 40 new or added lines in 4 files covered. (100.0%)

15386 of 54810 relevant lines covered (28.07%)

20267.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.87
/ui/src/apputil.cpp
1
/*
2
  Q Light Controller
3
  apputil.cpp
4

5
  Copyright (c) Heikki Junnila
6

7
  Licensed under the Apache License, Version 2.0 (the "License");
8
  you may not use this file except in compliance with the License.
9
  You may obtain a copy of the License at
10

11
      http://www.apache.org/licenses/LICENSE-2.0.txt
12

13
  Unless required by applicable law or agreed to in writing, software
14
  distributed under the License is distributed on an "AS IS" BASIS,
15
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
  See the License for the specific language governing permissions and
17
  limitations under the License.
18
*/
19

20
#include <QComboBox>
21
#include <QStyleFactory>
22
#include <QApplication>
23
#include <QTextStream>
24
#include <QSettings>
25
#include <QLocale>
26
#include <QWidget>
27
#include <QScreen>
28
#include <QStyle>
29
#include <QRect>
30
#include <QFile>
31
#include <QDir>
32

33
#include "apputil.h"
34
#include "qlcconfig.h"
35

36
/****************************************************************************
37
 * Widget visibility helper
38
 ****************************************************************************/
39

40
void AppUtil::ensureWidgetIsVisible(QWidget *widget)
7✔
41
{
42
    if (widget == NULL)
7✔
43
        return;
×
44

45
    QWidget *parent = widget->parentWidget();
7✔
46
    if (widget->windowFlags() & Qt::Window)
7✔
47
    {
48
        // The widget is a top-level window (a dialog, for instance)
49
        // @todo Use the screen where the main application currently is?
50
        QScreen *screen = QGuiApplication::screens().first();
7✔
51
        if (screen != NULL)
7✔
52
        {
53
            // Move the widget to the center of the default screen
54
            const QRect screenRect(screen->availableGeometry());
7✔
55
            if (screenRect.contains(widget->pos()) == false)
7✔
56
            {
57
                QRect widgetRect(widget->rect());
×
58
                widgetRect.moveCenter(screenRect.center());
×
59
                widget->setGeometry(widgetRect);
×
60
            }
61
        }
62
        else
63
        {
64
            // Last resort: move to top left and hope the widget is visible
65
            widget->move(0, 0);
×
66
        }
67
    }
68
    else if (parent != NULL)
×
69
    {
70
        // The widget's placement is bounded by a parent
71
        const QRect parentRect(parent->rect());
×
72
        if (parentRect.contains(widget->pos()) == false)
×
73
        {
74
            // Move the widget to the center of the parent if wouldn't
75
            // otherwise be visible
76
            QRect widgetRect(widget->rect());
×
77
            widgetRect.moveCenter(parentRect.center());
×
78
            widget->setGeometry(widgetRect);
×
79
        }
80
    }
81
}
82

83
/*****************************************************************************
84
 * Sane style
85
 *****************************************************************************/
86

87
#define SETTINGS_SLIDERSTYLE "workspace/sliderstyle"
88

89
static QStyle* s_saneStyle = NULL;
90

91
QStyle* AppUtil::saneStyle()
51✔
92
{
93
    if (s_saneStyle == NULL)
51✔
94
    {
95
        QSettings settings;
8✔
96
        QVariant var = settings.value(SETTINGS_SLIDERSTYLE, QString("Fusion"));
12✔
97
        QStringList keys(QStyleFactory::keys());
8✔
98

99
        if (keys.contains(var.toString()) == true)
4✔
100
            s_saneStyle = QStyleFactory::create(var.toString());
4✔
101
        else
102
            s_saneStyle = QApplication::style();
×
103
    }
104

105
    return s_saneStyle;
51✔
106
}
107

108
/*********************************************************************
109
 * Stylesheets
110
 *********************************************************************/
111

112
#define USER_STYLESHEET_FILE "qlcplusStyle.qss"
113

114
QString AppUtil::getStyleSheet(QString component)
76✔
115
{
116
    QString ssDir;
152✔
117
    QString result;
76✔
118

119
#if defined(WIN32) || defined(Q_OS_WIN)
120
    /* User's input profile directory on Windows */
121
    LPTSTR home = (LPTSTR) malloc(256 * sizeof(TCHAR));
122
    GetEnvironmentVariable(TEXT("UserProfile"), home, 256);
123
    ssDir = QString("%1/%2").arg(QString::fromUtf16(reinterpret_cast<char16_t*> (home)))
124
                .arg(USERQLCPLUSDIR);
125
    free(home);
126
    HotPlugMonitor::setWinId(winId());
127
#else
128
    /* User's input profile directory on *NIX systems */
129
    ssDir = QString("%1/%2").arg(getenv("HOME")).arg(USERQLCPLUSDIR);
76✔
130
#endif
131

132
    QFile ssFile(ssDir + QDir::separator() + USER_STYLESHEET_FILE);
228✔
133
    if (ssFile.exists() == false)
76✔
134
        return result;
76✔
135

136
    if (ssFile.open(QIODevice::ReadOnly) == false)
×
137
        return result;
×
138

139
    bool found = false;
×
140
    QTextStream in(&ssFile);
×
141
    while (!in.atEnd())
×
142
    {
143
        QString line = in.readLine();
×
144
        if (line.startsWith("====="))
×
145
        {
146
            if (found == true)
×
147
                break;
×
148

149
            QString comp = line.replace("=", "");
×
150
            if (comp.simplified() == component)
×
151
                found = true;
×
152
        }
153
        else if (found == true)
×
154
        {
155
            result.append(line);
×
156
        }
157
    }
158
    ssFile.close();
×
159

160
    return result;
×
161
}
162

163
/*****************************************************************************
164
 * Digits
165
 *****************************************************************************/
166

167
unsigned int AppUtil::digits(unsigned int n)
×
168
{
169
    unsigned int res = 1;
×
170
    while (n /= 10)
×
171
        ++res;
×
172
    return res;
×
173
}
174

175
/*****************************************************************************
176
 * ComboBoxDelegate
177
 *****************************************************************************/
178

179
ComboBoxDelegate::ComboBoxDelegate(const QStringList &strings, QWidget *parent)
×
180
    : QStyledItemDelegate(parent)
181
    , m_strings(strings)
×
182
{
183
}
×
184

185
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
×
186
        const QStyleOptionViewItem &/*option*/,
187
        const QModelIndex &/*index*/) const
188
{
189
    QComboBox *comboBox = new QComboBox(parent);
×
190
    comboBox->addItems(m_strings);
×
191
    return comboBox;
×
192
}
193

194
void ComboBoxDelegate::setEditorData(QWidget *editor,
×
195
        const QModelIndex &index) const
196
{
197
    int value = index.model()->data(index, Qt::UserRole).toInt();
×
198
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
×
199
    comboBox->setCurrentIndex(value);
×
200
}
×
201

202
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
×
203
        const QModelIndex &index) const
204
{
205
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
×
206
    int value = comboBox->currentIndex();
×
207
    model->setData(index, value, Qt::UserRole);
×
208
    model->setData(index, comboBox->currentText(), Qt::DisplayRole);
×
209
}
×
210

211
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
×
212
        const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
213
{
214
    editor->setGeometry(option.rect);
×
215
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc