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

mcallegari / qlcplus / 8961243534

05 May 2024 09:23PM UTC coverage: 32.068% (+4.0%) from 28.094%
8961243534

push

github

mcallegari
Merge branch 'master' into qmltoqt6

902 of 2557 new or added lines in 140 files covered. (35.28%)

166 existing lines in 76 files now uncovered.

15395 of 48008 relevant lines covered (32.07%)

22949.67 hits per line

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

30.12
/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 <QDebug>
29
#include <QStyle>
30
#include <QRect>
31
#include <QFile>
32
#include <QDir>
33

34
#if defined(WIN32) || defined(Q_OS_WIN)
35
#include <windows.h>
36
#include <lmcons.h>
37
#endif
38

39
#include "apputil.h"
40
#include "qlcconfig.h"
41

42
/****************************************************************************
43
 * Widget visibility helper
44
 ****************************************************************************/
45

46
void AppUtil::ensureWidgetIsVisible(QWidget *widget)
7✔
47
{
48
    if (widget == NULL)
7✔
49
        return;
×
50

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

89
/*****************************************************************************
90
 * Sane style
91
 *****************************************************************************/
92

93
#define SETTINGS_SLIDERSTYLE "workspace/sliderstyle"
94

95
static QStyle* s_saneStyle = NULL;
96

97
QStyle* AppUtil::saneStyle()
51✔
98
{
99
    if (s_saneStyle == NULL)
51✔
100
    {
101
        QSettings settings;
8✔
102
        QVariant var = settings.value(SETTINGS_SLIDERSTYLE, QString("Fusion"));
12✔
103
        QStringList keys(QStyleFactory::keys());
8✔
104

105
        if (keys.contains(var.toString()) == true)
4✔
106
            s_saneStyle = QStyleFactory::create(var.toString());
4✔
107
        else
108
            s_saneStyle = QApplication::style();
×
109
    }
110

111
    return s_saneStyle;
51✔
112
}
113

114
/*********************************************************************
115
 * Stylesheets
116
 *********************************************************************/
117

118
#define USER_STYLESHEET_FILE "qlcplusStyle.qss"
119

120
bool styleCached = false;
121
QMap<QString,QString> styleCache;
122

123
QString AppUtil::getStyleSheet(QString component)
76✔
124
{
125
    QString block;
152✔
126

127
    if (styleCached == false)
76✔
128
    {
129
#if defined(WIN32) || defined(Q_OS_WIN)
130
        /* User's input profile directory on Windows */
131
        LPTSTR home = (LPTSTR) malloc(256 * sizeof(TCHAR));
132
        GetEnvironmentVariable(TEXT("UserProfile"), home, 256);
133
        QString ssDir = QString("%1/%2").arg(QString::fromUtf16(reinterpret_cast<char16_t*> (home)))
134
                    .arg(USERQLCPLUSDIR);
135
        free(home);
136
#else
137
        /* User's input profile directory on *NIX systems */
138
        QString ssDir = QString("%1/%2").arg(getenv("HOME")).arg(USERQLCPLUSDIR);
14✔
139
#endif
140

141
        styleCached = true;
7✔
142

143
        QFile ssFile(ssDir + QDir::separator() + USER_STYLESHEET_FILE);
14✔
144
        if (ssFile.exists() == false)
7✔
145
            return block;
7✔
146

NEW
147
        if (ssFile.open(QIODevice::ReadOnly) == false)
×
NEW
148
            return block;
×
149

NEW
150
        bool found = false;
×
NEW
151
        QString compName;
×
NEW
152
        QTextStream in(&ssFile);
×
153

NEW
154
        while (!in.atEnd())
×
155
        {
NEW
156
            QString line = in.readLine();
×
NEW
157
            if (line.startsWith("====="))
×
158
            {
NEW
159
                if (found == true)
×
160
                {
NEW
161
                    styleCache.insert(compName, block);
×
NEW
162
                    block = "";
×
NEW
163
                    found = false;
×
164
                }
165

NEW
166
                compName = line.replace("=", "").simplified();
×
NEW
167
                qDebug() << "[AppUtil] found user style component:" << compName;
×
NEW
168
                found = true;
×
169
            }
NEW
170
            else if (found == true)
×
171
            {
NEW
172
                block.append(line);
×
173
            }
174
        }
NEW
175
        ssFile.close();
×
NEW
176
        if (found == true)
×
NEW
177
            styleCache.insert(compName, block);
×
178
    }
179

180
    return styleCache.value(component, QString());
138✔
181
}
182

183
/*****************************************************************************
184
 * Digits
185
 *****************************************************************************/
186

187
unsigned int AppUtil::digits(unsigned int n)
×
188
{
189
    unsigned int res = 1;
×
190
    while (n /= 10)
×
191
        ++res;
×
192
    return res;
×
193
}
194

195
/*****************************************************************************
196
 * ComboBoxDelegate
197
 *****************************************************************************/
198

199
ComboBoxDelegate::ComboBoxDelegate(const QStringList &strings, QWidget *parent)
×
200
    : QStyledItemDelegate(parent)
201
    , m_strings(strings)
×
202
{
203
}
×
204

205
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
×
206
        const QStyleOptionViewItem &/*option*/,
207
        const QModelIndex &/*index*/) const
208
{
209
    QComboBox *comboBox = new QComboBox(parent);
×
210
    comboBox->addItems(m_strings);
×
211
    return comboBox;
×
212
}
213

214
void ComboBoxDelegate::setEditorData(QWidget *editor,
×
215
        const QModelIndex &index) const
216
{
217
    int value = index.model()->data(index, Qt::UserRole).toInt();
×
218
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
×
219
    comboBox->setCurrentIndex(value);
×
220
}
×
221

222
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
×
223
        const QModelIndex &index) const
224
{
225
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
×
226
    int value = comboBox->currentIndex();
×
227
    model->setData(index, value, Qt::UserRole);
×
228
    model->setData(index, comboBox->currentText(), Qt::DisplayRole);
×
229
}
×
230

231
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
×
232
        const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
233
{
234
    editor->setGeometry(option.rect);
×
235
}
×
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