1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright (c) 2010 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "QtStatusWidget.h"
#include <QBoxLayout>
#include <QComboBox>
#include <QLabel>
#include <QFrame>
#include <QPoint>
#include <QStackedWidget>
#include <QApplication>
#include <QDesktopWidget>
#include <qdebug.h>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMovie>
#include "Swift/QtUI/QtElidingLabel.h"
#include "Swift/QtUI/QtLineEdit.h"
#include "Swift/QtUI/QtSwiftUtil.h"
#include <Swift/Controllers/StatusUtil.h>
namespace Swift {
const QString QtStatusWidget::NO_MESSAGE = QString(QT_TRANSLATE_NOOP("QtStatusWidget", "(No message)"));
QtStatusWidget::QtStatusWidget(QWidget *parent) : QWidget(parent), editCursor_(Qt::IBeamCursor), viewCursor_(Qt::PointingHandCursor) {
isClicking_ = false;
connecting_ = false;
setMaximumHeight(24);
connectingMovie_ = new QMovie(":/icons/connecting.mng");
QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0,0,0,0);
stack_ = new QStackedWidget(this);
stack_->setLineWidth(2);
stack_->setFrameShape(QFrame::StyledPanel);
mainLayout->addWidget(stack_);
QWidget* page1 = new QWidget(this);
stack_->addWidget(page1);
QHBoxLayout* page1Layout = new QHBoxLayout(page1);
page1Layout->setSpacing(0);
page1Layout->setContentsMargins(0,0,0,0);
page1->setCursor(viewCursor_);
statusIcon_ = new QLabel(this);
statusIcon_->setMinimumSize(16, 16);
statusIcon_->setMaximumSize(16, 16);
page1Layout->addWidget(statusIcon_);
statusTextLabel_ = new QtElidingLabel(this);
QFont font = statusTextLabel_->font();
font.setItalic(true);
statusTextLabel_->setFont(font);
page1Layout->addWidget(statusTextLabel_);
icons_[StatusShow::Online] = QIcon(":/icons/online.png");
icons_[StatusShow::Away] = QIcon(":/icons/away.png");
icons_[StatusShow::DND] = QIcon(":/icons/dnd.png");
icons_[StatusShow::None] = QIcon(":/icons/offline.png");
setStatusType(StatusShow::None);
QWidget* page2 = new QWidget(this);
QHBoxLayout* page2Layout = new QHBoxLayout(page2);
page2Layout->setSpacing(0);
page2Layout->setContentsMargins(0,0,0,0);
stack_->addWidget(page2);
statusEdit_ = new QtLineEdit(this);
page2Layout->addWidget(statusEdit_);
connect(statusEdit_, SIGNAL(returnPressed()), this, SLOT(handleEditComplete()));
connect(statusEdit_, SIGNAL(escapePressed()), this, SLOT(handleEditCancelled()));
connect(statusEdit_, SIGNAL(textChanged(const QString&)), this, SLOT(generateList()));
setStatusText("");
menu_ = new QListWidget();
menu_->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint );
menu_->setAlternatingRowColors(true);
menu_->setFocusProxy(statusEdit_);
menu_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
menu_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QSizePolicy policy(menu_->sizePolicy());
policy.setVerticalPolicy(QSizePolicy::Expanding);
menu_->setSizePolicy(policy);
connect(menu_, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(handleItemClicked(QListWidgetItem*)));
viewMode();
}
QtStatusWidget::~QtStatusWidget() {
delete menu_;
delete connectingMovie_;
}
void QtStatusWidget::handleApplicationFocusChanged(QWidget* /*old*/, QWidget* /*now*/) {
QWidget* now = qApp->focusWidget();
if (!editing_ || stack_->currentIndex() == 0) {
return;
}
if (!now || (now != menu_ && now != statusEdit_ && !now->isAncestorOf(statusEdit_) && !now->isAncestorOf(menu_) && !statusEdit_->isAncestorOf(now) && !menu_->isAncestorOf(now))) {
handleEditCancelled();
}
}
void QtStatusWidget::mousePressEvent(QMouseEvent*) {
if (stack_->currentIndex() == 0) {
handleClicked();
}
}
void QtStatusWidget::generateList() {
if (!editing_) {
return;
}
QString text = statusEdit_->text();
newStatusText_ = text;
menu_->clear();
foreach (StatusShow::Type type, icons_.keys()) {
QListWidgetItem* item = new QListWidgetItem(text == "" ? NO_MESSAGE : text, menu_);
item->setIcon(icons_[type]);
item->setToolTip(P2QSTRING(statusShowTypeToFriendlyName(type)) + ": " + item->text());
item->setStatusTip(item->toolTip());
item->setData(Qt::UserRole, QVariant(type));
}
foreach (StatusShow::Type type, icons_.keys()) {
QListWidgetItem* item = new QListWidgetItem(P2QSTRING(statusShowTypeToFriendlyName(type)), menu_);
item->setIcon(icons_[type]);
item->setToolTip(item->text());
item->setStatusTip(item->toolTip());
item->setData(Qt::UserRole, QVariant(type));
}
}
void QtStatusWidget::handleClicked() {
editing_ = true;
QDesktopWidget* desktop = QApplication::desktop();
int screen = desktop->screenNumber(this);
QPoint point = mapToGlobal(QPoint(0, height()));
QRect geometry = desktop->availableGeometry(screen);
int x = point.x();
int y = point.y();
int width = 200;
int height = 80;
int screenWidth = geometry.x() + geometry.width();
if (x + width > screenWidth) {
x = screenWidth - width;
}
std::vector<StatusShow::Type> types;
types.push_back(StatusShow::Online);
types.push_back(StatusShow::FFC);
types.push_back(StatusShow::Away);
types.push_back(StatusShow::XA);
types.push_back(StatusShow::DND);
types.push_back(StatusShow::None);
foreach (StatusShow::Type type, types) {
if (statusEdit_->text() == P2QSTRING(statusShowTypeToFriendlyName(type))) {
statusEdit_->setText("");
}
}
generateList();
height = menu_->sizeHintForRow(0) * menu_->count();
int marginLeft;
int marginTop;
int marginRight;
int marginBottom;
menu_->getContentsMargins(&marginLeft, &marginTop, &marginRight, &marginBottom);
height += marginTop + marginBottom;
width += marginLeft + marginRight;
menu_->setGeometry(x, y, width, height);
menu_->move(x, y);
menu_->setMaximumWidth(width);
menu_->show();
activateWindow();
statusEdit_->selectAll();
stack_->setCurrentIndex(1);
statusEdit_->setFocus();
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(handleApplicationFocusChanged(QWidget*, QWidget*)), Qt::QueuedConnection);
}
void QtStatusWidget::viewMode() {
disconnect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(handleApplicationFocusChanged(QWidget*, QWidget*)));
editing_ = false;
menu_->hide();
stack_->setCurrentIndex(0);
}
void QtStatusWidget::handleEditComplete() {
editing_ = false;
statusText_ = newStatusText_;
viewMode();
emit onChangeStatusRequest(selectedStatusType_, statusText_);
}
void QtStatusWidget::handleEditCancelled() {
editing_ = false;
setStatusText(statusText_);
viewMode();
}
StatusShow::Type QtStatusWidget::getSelectedStatusShow() {
return selectedStatusType_;
}
void QtStatusWidget::handleItemClicked(QListWidgetItem* item) {
editing_ = false;
selectedStatusType_ = static_cast<StatusShow::Type>(item->data(Qt::UserRole).toInt());
QString message = item->data(Qt::DisplayRole).toString();
newStatusText_ = message == NO_MESSAGE ? "" : message;
statusEdit_->setText(newStatusText_);
handleEditComplete();
}
void QtStatusWidget::setNewToolTip() {
if (connecting_) {
statusTextLabel_->setToolTip(tr("Connecting"));
} else {
statusTextLabel_->setToolTip(P2QSTRING(statusShowTypeToFriendlyName(selectedStatusType_)) + ": " + statusTextLabel_->text());
}
}
void QtStatusWidget::setStatusText(const QString& text) {
connectingMovie_->stop();
statusText_ = text;
statusEdit_->setText(text);
QString escapedText(text.isEmpty() ? NO_MESSAGE : text);
escapedText.replace("<","<");
// statusTextLabel_->setText("<i>" + escapedText + "</i>");
statusTextLabel_->setText(escapedText);
setNewToolTip();
}
void QtStatusWidget::setConnecting() {
connecting_ = true;
statusIcon_->setMovie(connectingMovie_);
connectingMovie_->start();
setNewToolTip();
}
void QtStatusWidget::setStatusType(StatusShow::Type type) {
connecting_ = false;
selectedStatusType_ = icons_.contains(type) ? type : StatusShow::Online;
statusIcon_->setPixmap(icons_[selectedStatusType_].pixmap(16, 16));
setNewToolTip();
}
}
|