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

mcallegari / qlcplus / 7252848206

18 Dec 2023 07:26PM UTC coverage: 32.067% (+0.001%) from 32.066%
7252848206

push

github

mcallegari
Code style review #1427

199 of 628 new or added lines in 101 files covered. (31.69%)

8 existing lines in 2 files now uncovered.

15169 of 47304 relevant lines covered (32.07%)

23733.74 hits per line

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

0.0
/ui/src/docbrowser.cpp
1
/*
2
  Q Light Controller
3
  docbrowser.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 <QDesktopServices>
21
#include <QGestureEvent>
22
#include <QSwipeGesture>
23
#include <QApplication>
24
#include <QTextBrowser>
25
#include <QVBoxLayout>
26
#include <QMessageBox>
27
#include <QSettings>
28
#include <QToolBar>
29
#include <QScreen>
30
#include <QAction>
31
#include <QDebug>
32
#include <QIcon>
33
#include <QUrl>
34

35
#include "docbrowser.h"
36
#include "qlcconfig.h"
37
#include "qlcfile.h"
38
#include "apputil.h"
39

40
#define SETTINGS_GEOMETRY "documentbrowser/geometry"
41
#define HYSTERESIS_MS 100
42

43
/****************************************************************************
44
 * QLCTextBrowser
45
 ****************************************************************************/
46

47
QLCTextBrowser::QLCTextBrowser(QWidget* parent)
×
48
    : QTextBrowser(parent)
×
49
{
50
    grabGesture(Qt::SwipeGesture);
×
51
    m_hysteresis.start();
×
52
}
×
53

54
QLCTextBrowser::~QLCTextBrowser()
×
55
{
56
}
×
57

58
bool QLCTextBrowser::event(QEvent* ev)
×
59
{
60
    if (ev->type() == QEvent::Gesture)
×
61
    {
62
        QGestureEvent* gesture = static_cast<QGestureEvent*> (ev);
×
63
        QSwipeGesture* swipe = qobject_cast<QSwipeGesture*> (
×
64
            gesture->gesture(Qt::SwipeGesture));
×
65
        if (swipe == NULL)
×
66
        {
67
            /* NOP */
68
        }
69
        else if (swipe->horizontalDirection() == QSwipeGesture::Left)
×
70
        {
71
            if (m_hysteresis.elapsed() > HYSTERESIS_MS)
×
72
            {
73
                backward();
×
74
                ev->accept();
×
75
                m_hysteresis.start();
×
76
            }
77
        }
78
        else if (swipe->horizontalDirection() == QSwipeGesture::Right)
×
79
        {
80
            if (m_hysteresis.elapsed() > HYSTERESIS_MS)
×
81
            {
82
                forward();
×
83
                ev->accept();
×
84
                m_hysteresis.start();
×
85
            }
86
        }
87
    }
88

89
    return QTextBrowser::event(ev);
×
90
}
91

92
/****************************************************************************
93
 * DocBrowser
94
 ****************************************************************************/
95
DocBrowser* DocBrowser::s_instance = NULL;
96

97
DocBrowser::DocBrowser(QWidget* parent)
×
98
    : QWidget(parent, Qt::Window)
99
    , m_backwardAction(NULL)
100
    , m_forwardAction(NULL)
101
    , m_homeAction(NULL)
102
    , m_aboutQtAction(NULL)
×
103
{
104
    new QVBoxLayout(this);
×
105

106
    setWindowTitle(tr("%1 - Document Browser").arg(APPNAME));
×
107
    setWindowIcon(QIcon(":/help.png"));
×
108

109
    /* Recall window size */
110
    QSettings settings;
×
111
    QVariant var = settings.value(SETTINGS_GEOMETRY);
×
112
    if (var.isValid() == true)
×
113
    {
114
        restoreGeometry(var.toByteArray());
×
115
    }
116
    else
117
    {
118
        QScreen *screen = QGuiApplication::screens().first();
×
119
        QRect rect = screen->availableGeometry();
×
120
        int rWd = rect.width() / 4;
×
121
        int rHd = rect.height() / 4;
×
122
        resize(rWd * 3, rHd * 3);
×
123
        move(rWd / 2, rHd / 2);
×
124
    }
125
    AppUtil::ensureWidgetIsVisible(this);
×
126

127
    /* Actions */
128
    m_backwardAction = new QAction(QIcon(":/back.png"), tr("Backward"), this);
×
129
    m_forwardAction = new QAction(QIcon(":/forward.png"), tr("Forward"), this);
×
130
    m_homeAction = new QAction(QIcon(":/qlcplus.png"), tr("Index"), this);
×
131
    m_aboutQtAction = new QAction(QIcon(":/qt.png"), tr("About Qt"), this);
×
132
    m_closeAction = new QAction(QIcon(":/delete.png"), tr("Close this window"), this);
×
133

134
    m_backwardAction->setEnabled(false);
×
135
    m_forwardAction->setEnabled(false);
×
136

137
    QAction* action = new QAction(this);
×
138
    action->setShortcut(QKeySequence(QKeySequence::Close));
×
139
    connect(action, SIGNAL(triggered(bool)), this, SLOT(close()));
×
140
    addAction(action);
×
141

142
    /* Toolbar */
143
    m_toolbar = new QToolBar("Document Browser", this);
×
144
    layout()->addWidget(m_toolbar);
×
145
    m_toolbar->addAction(m_backwardAction);
×
146
    m_toolbar->addAction(m_forwardAction);
×
147
    m_toolbar->addAction(m_homeAction);
×
148
    QWidget* widget = new QWidget(this);
×
149
    widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
×
150
    m_toolbar->addWidget(widget);
×
151
    m_toolbar->addAction(m_aboutQtAction);
×
152

153
    /* Browser */
154
    m_browser = new QLCTextBrowser(this);
×
155
    // Override the link handler to only assume http/https are external links
156
    // This may be replaced with setOpenExternalLinks(true) and slotAnchorClicked removed if
157
    // Qt fixes setOpenExternalLinks to work with relative URL references
158
    m_browser->setOpenLinks(false);
×
159
    layout()->addWidget(m_browser);
×
160

161
    connect(m_browser, SIGNAL(backwardAvailable(bool)),
×
162
            this, SLOT(slotBackwardAvailable(bool)));
163
    connect(m_browser, SIGNAL(forwardAvailable(bool)),
×
164
            this, SLOT(slotForwardAvailable(bool)));
165
    connect(m_backwardAction, SIGNAL(triggered(bool)),
×
166
            m_browser, SLOT(backward()));
×
167
    connect(m_forwardAction, SIGNAL(triggered(bool)),
×
168
            m_browser, SLOT(forward()));
×
169
    connect(m_homeAction, SIGNAL(triggered(bool)),
×
170
            m_browser, SLOT(home()));
×
171
    connect(m_aboutQtAction, SIGNAL(triggered(bool)),
×
172
            this, SLOT(slotAboutQt()));
173
    connect(m_browser, SIGNAL(anchorClicked(QUrl)), this, SLOT(slotAnchorClicked(QUrl)));
×
174
    if (QLCFile::hasWindowManager() == false)
×
175
    {
176
        m_toolbar->addAction(m_closeAction);
×
177
        connect(m_closeAction, SIGNAL(triggered(bool)),
×
178
                this, SLOT(slotCloseWindow()));
179
    }
180

181
    /* Set document search paths */
182
    QStringList searchPaths;
×
183
    searchPaths << QLCFile::systemDirectory(QString("%1/html/").arg(DOCSDIR)).path();
×
184

185
    m_browser->setSearchPaths(searchPaths);
×
186
    m_browser->setSource(QUrl("file:index.html"));
×
187
}
×
188

189
DocBrowser::~DocBrowser()
×
190
{
191
    QSettings settings;
×
192
    settings.setValue(SETTINGS_GEOMETRY, saveGeometry());
×
193
    s_instance = NULL;
×
194
}
×
195

196
void DocBrowser::slotAnchorClicked(QUrl url){
×
NEW
197
    if (url.scheme() == QLatin1String("http") || url.scheme() == QLatin1String("https"))
×
198
     {
199
        QDesktopServices::openUrl(url);
×
200
     }
201
     else
202
     {
203
        m_browser->setSource(url);
×
204
     }
205
}
×
206

207
void DocBrowser::createAndShow(QWidget* parent)
×
208
{
209
    if (s_instance == NULL)
×
210
    {
211
        s_instance = new DocBrowser(parent);
×
212
        s_instance->setAttribute(Qt::WA_DeleteOnClose);
×
213
    }
214

215
    s_instance->show();
×
216
    s_instance->raise();
×
217
}
×
218

219
void DocBrowser::slotBackwardAvailable(bool available)
×
220
{
221
    m_backwardAction->setEnabled(available);
×
222
}
×
223

224
void DocBrowser::slotForwardAvailable(bool available)
×
225
{
226
    m_forwardAction->setEnabled(available);
×
227
}
×
228

229
void DocBrowser::slotAboutQt()
×
230
{
231
    QMessageBox::aboutQt(this, QString(APPNAME));
×
232
}
×
233

234
void DocBrowser::slotCloseWindow()
×
235
{
236
    close();
×
237
}
×
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