1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * https://lxqt.org
6 *
7 * Copyright: 2015 LXQt team
8 * Authors:
9 * Dmitriy Zhukov <zjesclean@gmail.com>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General
21 * Public License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 *
25 * END_COMMON_COPYRIGHT_HEADER */
26
27 #include <QBoxLayout>
28 #include <QLabel>
29 #include <QDebug>
30 #include <QEvent>
31 #include <QIcon>
32 #include <QToolButton>
33 #include <QFileInfo>
34 #include "kbdstate.h"
35 #include "content.h"
36
Content(bool layoutEnabled)37 Content::Content(bool layoutEnabled):
38 QWidget(),
39 m_layoutEnabled(layoutEnabled)
40 {
41 QBoxLayout *box = new QBoxLayout(QBoxLayout::LeftToRight);
42 box->setContentsMargins(0, 0, 0, 0);
43 box->setSpacing(0);
44 setLayout(box);
45
46 m_capsLock = new QLabel(tr("C", "Label for CapsLock indicator"));
47 m_capsLock->setObjectName(QStringLiteral("CapsLockLabel"));
48 m_capsLock->setAlignment(Qt::AlignCenter);
49 m_capsLock->setToolTip(tr("CapsLock", "Tooltip for CapsLock indicator"));
50 m_capsLock->installEventFilter(this);
51 layout()->addWidget(m_capsLock);
52
53 m_numLock = new QLabel(tr("N", "Label for NumLock indicator"));
54 m_numLock->setObjectName(QStringLiteral("NumLockLabel"));
55 m_numLock->setToolTip(tr("NumLock", "Tooltip for NumLock indicator"));
56 m_numLock->setAlignment(Qt::AlignCenter);
57 m_numLock->installEventFilter(this);
58 layout()->addWidget(m_numLock);
59
60 m_scrollLock = new QLabel(tr("S", "Label for ScrollLock indicator"));
61 m_scrollLock->setObjectName(QStringLiteral("ScrollLockLabel"));
62 m_scrollLock->setToolTip(tr("ScrollLock", "Tooltip for ScrollLock indicator"));
63 m_scrollLock->setAlignment(Qt::AlignCenter);
64 m_scrollLock->installEventFilter(this);
65 layout()->addWidget(m_scrollLock);
66
67 m_layout = new QToolButton;
68 m_layout->setObjectName(QStringLiteral("LayoutLabel"));
69 m_layout->setAutoRaise(true);
70 connect(m_layout, &QAbstractButton::released, this, [this] { emit controlClicked(Controls::Layout); });
71 box->addWidget(m_layout, 0, Qt::AlignCenter);
72 }
73
74 Content::~Content() = default;
75
setup()76 bool Content::setup()
77 {
78 m_capsLock->setVisible(Settings::instance().showCapLock());
79 m_numLock->setVisible(Settings::instance().showNumLock());
80 m_scrollLock->setVisible(Settings::instance().showScrollLock());
81 m_layout->setVisible(m_layoutEnabled && Settings::instance().showLayout());
82 m_layoutFlagPattern = Settings::instance().layoutFlagPattern();
83 return true;
84 }
85
layoutChanged(const QString & sym,const QString & name,const QString & variant)86 void Content::layoutChanged(const QString & sym, const QString & name, const QString & variant)
87 {
88 m_layout->setText(sym.toUpper());
89 QString flag_file;
90 if (m_layoutFlagPattern.contains(QStringLiteral("%1")))
91 flag_file = m_layoutFlagPattern.arg(sym);
92 if (flag_file.isEmpty() || !QFileInfo::exists(flag_file))
93 {
94 m_layout->setToolButtonStyle(Qt::ToolButtonTextOnly);
95 m_layout->setIcon({});
96 } else
97 {
98 m_layout->setIcon(QIcon{flag_file});
99 m_layout->setToolButtonStyle(m_layout->icon().pixmap(m_layout->iconSize()).isNull() ? Qt::ToolButtonTextOnly : Qt::ToolButtonIconOnly);
100 }
101 QString txt = QStringLiteral("<html><table>\
102 <tr><td>%1: </td><td>%3</td></tr>\
103 <tr><td>%2: </td><td>%4</td></tr>\
104 </table></html>").arg(tr("Layout")).arg(tr("Variant")).arg(name).arg(variant);
105 m_layout->setToolTip(txt);
106 }
107
modifierStateChanged(Controls mod,bool active)108 void Content::modifierStateChanged(Controls mod, bool active)
109 {
110 setEnabled(mod, active);
111 }
112
113
setEnabled(Controls cnt,bool enabled)114 void Content::setEnabled(Controls cnt, bool enabled)
115 {
116 widget(cnt)->setEnabled(enabled);
117 }
118
widget(Controls cnt) const119 QWidget* Content::widget(Controls cnt) const
120 {
121 switch(cnt){
122 case Caps: return m_capsLock;
123 case Num: return m_numLock;
124 case Scroll: return m_scrollLock;
125 case Layout: return m_layout;
126 }
127 return nullptr;
128 }
129
eventFilter(QObject * object,QEvent * event)130 bool Content::eventFilter(QObject *object, QEvent *event)
131 {
132 if (event->type() == QEvent::QEvent::MouseButtonRelease)
133 {
134 if (object == m_capsLock)
135 emit controlClicked(Controls::Caps);
136 else if (object == m_numLock)
137 emit controlClicked(Controls::Num);
138 else if (object == m_scrollLock)
139 emit controlClicked(Controls::Scroll);
140 }
141
142 return QWidget::eventFilter(object, event);
143 }
144
showHorizontal()145 void Content::showHorizontal()
146 {
147 qobject_cast<QBoxLayout*>(layout())->setDirection(QBoxLayout::LeftToRight);
148 }
149
showVertical()150 void Content::showVertical()
151 {
152 qobject_cast<QBoxLayout*>(layout())->setDirection(QBoxLayout::TopToBottom);
153 }
154