RabbitCommon v2.2.6
Loading...
Searching...
No Matches
QUIWidget.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author Kang Lin <kl222@126.com>
3
4//#pragma execution_character_set("utf-8")
5
6#include "QUIWidget.h"
7#include <QLabel>
8#include <QPushButton>
9#include <QToolButton>
10#include <QBoxLayout>
11#include <QLineEdit>
12#include <QComboBox>
13#include <QAction>
14#include <QFile>
15#include <QEvent>
16#include <QTranslator>
17#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
18 #include <QScreen>
19#else
20 #include <QTextCodec>
21 #include <QDesktopWidget>
22#endif
23#include <QFontDatabase>
24#include <QApplication>
25#include <QTimer>
26#include <QDebug>
27#include <QMouseEvent>
28
29QUIWidget::QUIWidget(QWidget *parent, bool bQuitOnClose) : QDialog(parent)
30{
31 setAttribute(Qt::WA_QuitOnClose, bQuitOnClose);
32 this->initControl();
33 this->initForm();
34}
35
36QUIWidget::~QUIWidget()
37{
38 delete widgetMain;
39}
40
41void QUIWidget::setStyle(QUIWidget::Style style)
42{
43#if defined(DEBUG) || defined(_DEBUG)
44 QString qssFile = ":/qss/blue.css";
45
46 if (style == QUIWidget::Style_Silvery) {
47 qssFile = ":/qss/silvery.css";
48 } else if (style == QUIWidget::Style_Blue) {
49 qssFile = ":/qss/blue.css";
50 } else if (style == QUIWidget::Style_LightBlue) {
51 qssFile = ":/qss/lightblue.css";
52 } else if (style == QUIWidget::Style_DarkBlue) {
53 qssFile = ":/qss/darkblue.css";
54 } else if (style == QUIWidget::Style_Gray) {
55 qssFile = ":/qss/gray.css";
56 } else if (style == QUIWidget::Style_LightGray) {
57 qssFile = ":/qss/lightgray.css";
58 } else if (style == QUIWidget::Style_DarkGray) {
59 qssFile = ":/qss/darkgray.css";
60 } else if (style == QUIWidget::Style_Black) {
61 qssFile = ":/qss/black.css";
62 } else if (style == QUIWidget::Style_LightBlack) {
63 qssFile = ":/qss/lightblack.css";
64 } else if (style == QUIWidget::Style_DarkBlack) {
65 qssFile = ":/qss/darkblack.css";
66 } else if (style == QUIWidget::Style_PSBlack) {
67 qssFile = ":/qss/psblack.css";
68 } else if (style == QUIWidget::Style_FlatBlack) {
69 qssFile = ":/qss/flatblack.css";
70 } else if (style == QUIWidget::Style_FlatWhite) {
71 qssFile = ":/qss/flatwhite.css";
72 }
73
74 QFile file(qssFile);
75
76 if (file.open(QFile::ReadOnly)) {
77 QString qss = QLatin1String(file.readAll());
78 QString paletteColor = qss.mid(20, 7);
79 qApp->setPalette(QPalette(QColor(paletteColor)));
80 qApp->setStyleSheet(qss);
81 file.close();
82 }
83#endif
84}
85
86void QUIWidget::setStyle(const QString &qssFile)
87{
88 QFile file(qssFile);
89
90 if (file.open(QFile::ReadOnly)) {
91 QString qss = QLatin1String(file.readAll());
92 QString paletteColor = qss.mid(20, 7);
93 qApp->setPalette(QPalette(QColor(paletteColor)));
94 qApp->setStyleSheet(qss);
95 file.close();
96 }
97}
98
99void QUIWidget::setFormInCenter(QWidget *frm)
100{
101 Q_ASSERT(frm != nullptr);
102 int frmX = frm->width();
103 int frmY = frm->height();
104#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
105 QDesktopWidget w;
106 int deskWidth = w.availableGeometry().width();
107 int deskHeight = w.availableGeometry().height();
108#else
109 QScreen* pScreen = qApp->primaryScreen();
110 if(!pScreen) return;
111 int deskWidth = pScreen->availableGeometry().width();
112 int deskHeight = pScreen->availableGeometry().height();
113#endif
114 QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
115 frm->move(movePoint);
116}
117
118void QUIWidget::getQssColor(const QString &qss, QString &textColor, QString &panelColor, QString &borderColor,
119 QString &normalColorStart, QString &normalColorEnd,
120 QString &darkColorStart, QString &darkColorEnd, QString &highColor)
121{
122 QString str = qss;
123
124 QString flagTextColor = "TextColor:";
125 int indexTextColor = str.indexOf(flagTextColor);
126
127 if (indexTextColor >= 0) {
128 textColor = str.mid(indexTextColor + flagTextColor.length(), 7);
129 }
130
131 QString flagPanelColor = "PanelColor:";
132 int indexPanelColor = str.indexOf(flagPanelColor);
133
134 if (indexPanelColor >= 0) {
135 panelColor = str.mid(indexPanelColor + flagPanelColor.length(), 7);
136 }
137
138 QString flagBorderColor = "BorderColor:";
139 int indexBorderColor = str.indexOf(flagBorderColor);
140
141 if (indexBorderColor >= 0) {
142 borderColor = str.mid(indexBorderColor + flagBorderColor.length(), 7);
143 }
144
145 QString flagNormalColorStart = "NormalColorStart:";
146 int indexNormalColorStart = str.indexOf(flagNormalColorStart);
147
148 if (indexNormalColorStart >= 0) {
149 normalColorStart = str.mid(indexNormalColorStart + flagNormalColorStart.length(), 7);
150 }
151
152 QString flagNormalColorEnd = "NormalColorEnd:";
153 int indexNormalColorEnd = str.indexOf(flagNormalColorEnd);
154
155 if (indexNormalColorEnd >= 0) {
156 normalColorEnd = str.mid(indexNormalColorEnd + flagNormalColorEnd.length(), 7);
157 }
158
159 QString flagDarkColorStart = "DarkColorStart:";
160 int indexDarkColorStart = str.indexOf(flagDarkColorStart);
161
162 if (indexDarkColorStart >= 0) {
163 darkColorStart = str.mid(indexDarkColorStart + flagDarkColorStart.length(), 7);
164 }
165
166 QString flagDarkColorEnd = "DarkColorEnd:";
167 int indexDarkColorEnd = str.indexOf(flagDarkColorEnd);
168
169 if (indexDarkColorEnd >= 0) {
170 darkColorEnd = str.mid(indexDarkColorEnd + flagDarkColorEnd.length(), 7);
171 }
172
173 QString flagHighColor = "HighColor:";
174 int indexHighColor = str.indexOf(flagHighColor);
175
176 if (indexHighColor >= 0) {
177 highColor = str.mid(indexHighColor + flagHighColor.length(), 7);
178 }
179}
180
181void QUIWidget::setTranslator(const QString &qmFile)
182{
183 QTranslator *translator = new QTranslator(qApp);
184 if(translator->load(qmFile))
185 qApp->installTranslator(translator);
186}
187
188void QUIWidget::setCode()
189{
190#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
191 #if _MSC_VER
192 QTextCodec *codec = QTextCodec::codecForName("gbk");
193 #else
194 QTextCodec *codec = QTextCodec::codecForName("utf-8");
195 #endif
196 QTextCodec::setCodecForLocale(codec);
197 QTextCodec::setCodecForCStrings(codec);
198 QTextCodec::setCodecForTr(codec);
199#elif (QT_VERSION < QT_VERSION_CHECK(6,0,0))
200 QTextCodec *codec = QTextCodec::codecForName("utf-8");
201 QTextCodec::setCodecForLocale(codec);
202#endif
203}
204
205void QUIWidget::showMessageBoxInfo(const QString &info, int closeSec)
206{
207 QUIMessageBox::Instance()->setMessage(info, 0, closeSec);
208 QUIMessageBox::Instance()->show();
209}
210
211void QUIWidget::showMessageBoxError(const QString &info, int closeSec)
212{
213 QUIMessageBox::Instance()->setMessage(info, 2, closeSec);
214 QUIMessageBox::Instance()->show();
215}
216
217int QUIWidget::showMessageBoxQuestion(const QString &info)
218{
219 QUIMessageBox msg;
220 msg.setMessage(info, 1);
221 return msg.exec();
222}
223
224QString QUIWidget::showInputBox(bool &ok, const QString &title, int type, int closeSec,
225 QString defaultValue, bool pwd)
226{
227 QUIInputBox input;
228 input.setParameter(title, type, closeSec, defaultValue, pwd);
229 ok = input.exec();
230 return input.getValue();
231}
232
233bool QUIWidget::eventFilter(QObject *obj, QEvent *evt)
234{
235 static QPoint mousePoint;
236 static bool mousePressed = false;
237
238 QMouseEvent *event = static_cast<QMouseEvent *>(evt);
239 if (event->type() == QEvent::MouseButtonPress) {
240 if (event->button() == Qt::LeftButton) {
241 mousePressed = true;
242 mousePoint = event->globalPos() - this->pos();
243 return true;
244 }
245 } else if (event->type() == QEvent::MouseButtonRelease) {
246 mousePressed = false;
247 return true;
248 } else if (event->type() == QEvent::MouseMove) {
249 if (!max && mousePressed && (event->buttons() & Qt::LeftButton)) {
250 this->move(event->globalPos() - mousePoint);
251 return true;
252 }
253 } else if (event->type() == QEvent::MouseButtonDblClick) {
254 //以下写法可以将双击识别限定在标题栏
255 //if (this->btnMenu_Max->isVisible() && obj == this->widget_title) {
256 if (this->btnMenu_Max->isVisible()) {
257 this->on_btnMenu_Max_clicked();
258 return true;
259 }
260 } else if (event->type() == QEvent::Hide) {
261 if (obj == mainWidget) {
262 this->hide();
263 }
264 }
265
266 return QDialog::eventFilter(obj, evt);
267}
268
269QLabel *QUIWidget::getLabIco() const
270{
271 return this->lab_Ico;
272}
273
274QLabel *QUIWidget::getLabTitle() const
275{
276 return this->lab_Title;
277}
278
279QToolButton *QUIWidget::getBtnMenu() const
280{
281 return this->btnMenu;
282}
283
284QPushButton *QUIWidget::getBtnMenuMin() const
285{
286 return this->btnMenu_Min;
287}
288
289QPushButton *QUIWidget::getBtnMenuMax() const
290{
291 return this->btnMenu_Max;
292}
293
294QPushButton *QUIWidget::getBtnMenuMClose() const
295{
296 return this->btnMenu_Close;
297}
298
299QString QUIWidget::getTitle() const
300{
301 return this->title;
302}
303
304Qt::Alignment QUIWidget::getAlignment() const
305{
306 return this->alignment;
307}
308
309QSize QUIWidget::sizeHint() const
310{
311 return QSize(600, 450);
312}
313
314QSize QUIWidget::minimumSizeHint() const
315{
316 return QSize(200, 150);
317}
318
319void QUIWidget::initControl()
320{
321 this->setObjectName(QString::fromUtf8("QUIWidget"));
322 this->resize(900, 750);
323 verticalLayout1 = new QVBoxLayout(this);
324 verticalLayout1->setSpacing(0);
325 verticalLayout1->setContentsMargins(11, 11, 11, 11);
326 verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1"));
327 verticalLayout1->setContentsMargins(1, 1, 1, 1);
328 widgetMain = new QWidget(this);
329 widgetMain->setObjectName(QString::fromUtf8("widgetMain"));
330 widgetMain->setStyleSheet(QString::fromUtf8(""));
331 widgetMain->setFocusPolicy(Qt::NoFocus);
332 verticalLayout2 = new QVBoxLayout(widgetMain);
333 verticalLayout2->setSpacing(0);
334 verticalLayout2->setContentsMargins(11, 11, 11, 11);
335 verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2"));
336 verticalLayout2->setContentsMargins(0, 0, 0, 0);
337 widget_title = new QWidget(widgetMain);
338 widget_title->setObjectName(QString::fromUtf8("widget_title"));
339 widget_title->setFocusPolicy(Qt::NoFocus);
340 QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
341 sizePolicy.setHorizontalStretch(0);
342 sizePolicy.setVerticalStretch(0);
343 sizePolicy.setHeightForWidth(widget_title->sizePolicy().hasHeightForWidth());
344 widget_title->setSizePolicy(sizePolicy);
345 widget_title->setMinimumSize(QSize(0, 30));
346 horizontalLayout4 = new QHBoxLayout(widget_title);
347 horizontalLayout4->setSpacing(0);
348 horizontalLayout4->setContentsMargins(11, 11, 11, 11);
349 horizontalLayout4->setObjectName(QString::fromUtf8("horizontalLayout4"));
350 horizontalLayout4->setContentsMargins(0, 0, 0, 0);
351 lab_Ico = new QLabel(widget_title);
352 lab_Ico->setObjectName(QString::fromUtf8("lab_Ico"));
353 lab_Ico->setFocusPolicy(Qt::NoFocus);
354 QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred);
355 sizePolicy1.setHorizontalStretch(0);
356 sizePolicy1.setVerticalStretch(0);
357 sizePolicy1.setHeightForWidth(lab_Ico->sizePolicy().hasHeightForWidth());
358 lab_Ico->setSizePolicy(sizePolicy1);
359 lab_Ico->setMinimumSize(QSize(30, 0));
360 lab_Ico->setAlignment(Qt::AlignCenter);
361
362 horizontalLayout4->addWidget(lab_Ico);
363
364 lab_Title = new QLabel(widget_title);
365 lab_Title->setObjectName(QString::fromUtf8("lab_Title"));
366 QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred);
367 sizePolicy2.setHorizontalStretch(0);
368 sizePolicy2.setVerticalStretch(0);
369 sizePolicy2.setHeightForWidth(lab_Title->sizePolicy().hasHeightForWidth());
370 lab_Title->setSizePolicy(sizePolicy2);
371 lab_Title->setFocusPolicy(Qt::NoFocus);
372 lab_Title->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
373
374 horizontalLayout4->addWidget(lab_Title);
375
376 widget_menu = new QWidget(widget_title);
377 widget_menu->setObjectName(QString::fromUtf8("widget_menu"));
378 sizePolicy1.setHeightForWidth(widget_menu->sizePolicy().hasHeightForWidth());
379 widget_menu->setSizePolicy(sizePolicy1);
380 widget_menu->setFocusPolicy(Qt::NoFocus);
381 horizontalLayout = new QHBoxLayout(widget_menu);
382 horizontalLayout->setSpacing(0);
383 horizontalLayout->setContentsMargins(11, 11, 11, 11);
384 horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
385 horizontalLayout->setContentsMargins(0, 0, 0, 0);
386 btnMenu = new QToolButton(widget_menu);
387 btnMenu->setObjectName(QString::fromUtf8("btnMenu"));
388 QSizePolicy sizePolicy3(QSizePolicy::Fixed, QSizePolicy::Expanding);
389 sizePolicy3.setHorizontalStretch(0);
390 sizePolicy3.setVerticalStretch(0);
391 sizePolicy3.setHeightForWidth(btnMenu->sizePolicy().hasHeightForWidth());
392 btnMenu->setSizePolicy(sizePolicy3);
393 btnMenu->setMinimumSize(QSize(30, 0));
394 btnMenu->setMaximumSize(QSize(30, 16777215));
395 btnMenu->setFocusPolicy(Qt::NoFocus);
396 btnMenu->setPopupMode(QToolButton::InstantPopup);
397
398 horizontalLayout->addWidget(btnMenu);
399
400 btnMenu_Min = new QPushButton(widget_menu);
401 btnMenu_Min->setObjectName(QString::fromUtf8("btnMenu_Min"));
402 QSizePolicy sizePolicy4(QSizePolicy::Minimum, QSizePolicy::Expanding);
403 sizePolicy4.setHorizontalStretch(0);
404 sizePolicy4.setVerticalStretch(0);
405 sizePolicy4.setHeightForWidth(btnMenu_Min->sizePolicy().hasHeightForWidth());
406 btnMenu_Min->setSizePolicy(sizePolicy4);
407 btnMenu_Min->setMinimumSize(QSize(30, 0));
408 btnMenu_Min->setMaximumSize(QSize(30, 16777215));
409 btnMenu_Min->setCursor(QCursor(Qt::ArrowCursor));
410 btnMenu_Min->setFocusPolicy(Qt::NoFocus);
411
412 horizontalLayout->addWidget(btnMenu_Min);
413
414 btnMenu_Max = new QPushButton(widget_menu);
415 btnMenu_Max->setObjectName(QString::fromUtf8("btnMenu_Max"));
416 sizePolicy3.setHeightForWidth(btnMenu_Max->sizePolicy().hasHeightForWidth());
417 btnMenu_Max->setSizePolicy(sizePolicy3);
418 btnMenu_Max->setMinimumSize(QSize(30, 0));
419 btnMenu_Max->setMaximumSize(QSize(30, 16777215));
420 btnMenu_Max->setCursor(QCursor(Qt::ArrowCursor));
421 btnMenu_Max->setFocusPolicy(Qt::NoFocus);
422
423 horizontalLayout->addWidget(btnMenu_Max);
424
425 btnMenu_Close = new QPushButton(widget_menu);
426 btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close"));
427 sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth());
428 btnMenu_Close->setSizePolicy(sizePolicy3);
429 btnMenu_Close->setMinimumSize(QSize(30, 0));
430 btnMenu_Close->setMaximumSize(QSize(30, 16777215));
431 btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor));
432 btnMenu_Close->setFocusPolicy(Qt::NoFocus);
433
434 horizontalLayout->addWidget(btnMenu_Close);
435 horizontalLayout4->addWidget(widget_menu);
436 verticalLayout2->addWidget(widget_title);
437
438 widget = new QWidget(widgetMain);
439 widget->setObjectName(QString::fromUtf8("widget"));
440 widget->setFocusPolicy(Qt::NoFocus);
441 verticalLayout3 = new QVBoxLayout(widget);
442 verticalLayout3->setSpacing(0);
443 verticalLayout3->setContentsMargins(11, 11, 11, 11);
444 verticalLayout3->setObjectName(QString::fromUtf8("verticalLayout3"));
445 verticalLayout3->setContentsMargins(0, 0, 0, 0);
446
447 verticalLayout2->addWidget(widget);
448 verticalLayout1->addWidget(widgetMain);
449
450 connect(this->btnMenu_Min, SIGNAL(clicked(bool)), this, SLOT(on_btnMenu_Min_clicked()));
451 connect(this->btnMenu_Max, SIGNAL(clicked(bool)), this, SLOT(on_btnMenu_Max_clicked()));
452 connect(this->btnMenu_Close, SIGNAL(clicked(bool)), this, SLOT(on_btnMenu_Close_clicked()));
453}
454
455void QUIWidget::initForm()
456{
457 //设置图形字体
458 setIcon(QUIWidget::Lab_Ico, QChar(0xf072), 11);
459 setIcon(QUIWidget::BtnMenu, QChar(0xf0d7));
460 setIcon(QUIWidget::BtnMenu_Min, QChar(0xf068));
461 setIcon(QUIWidget::BtnMenu_Max, QChar(0xf096)); // 0xf067);
462 setIcon(QUIWidget::BtnMenu_Close, QChar(0xf00d));
463
464 //设置标题及对齐方式
465 setTitle("QUI Demo");
466 setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
467 setVisible(QUIWidget::BtnMenu, false);
468
469 mainWidget = 0;
470 max = false;
471 location = this->geometry();
472 this->setProperty("form", true);
473 this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
474
475 //绑定事件过滤器监听鼠标移动
476 this->installEventFilter(this);
477 this->widget_title->installEventFilter(this);
478
479#if defined(DEBUG) || defined(_DEBUG)
480 //添加换肤菜单
481 QStringList name;
482// name << "银色" << "蓝色" << "浅蓝色" << "深蓝色" << "灰色" << "浅灰色" << "深灰色" << "黑色"
483// << "浅黑色" << "深黑色" << "PS黑色" << "黑色扁平" << "白色扁平";
484 name << tr("Silvery") << tr("Blue") << tr("Light blue") << tr("Dark blue")
485 << tr("Gray") << tr("Light gray") << tr("Dark gray") << tr("Black")
486 << tr("Light black") << tr("Dark black") << tr("PS black")
487 << tr("Flat black") << tr("Flat white");
488 foreach (QString str, name) {
489 QAction *action = new QAction(str, this);
490 this->btnMenu->addAction(action);
491 connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle()));
492 }
493#endif
494}
495
496void QUIWidget::changeStyle()
497{
498 QAction *act = (QAction *)sender();
499 QString name = act->text();
500 QString qssFile = ":/qss/blue.css";
501
502 if (name == tr("Silvery")) {
503 qssFile = ":/qss/silvery.css";
504 setStyle(QUIWidget::Style_Silvery);
505 } else if (name == tr("Blue")) {
506 qssFile = ":/qss/blue.css";
507 setStyle(QUIWidget::Style_Blue);
508 } else if (name == tr("Light blue")) {
509 qssFile = ":/qss/lightblue.css";
510 setStyle(QUIWidget::Style_LightBlue);
511 } else if (name == tr("Dark blue")) {
512 qssFile = ":/qss/darkblue.css";
513 setStyle(QUIWidget::Style_DarkBlue);
514 } else if (name == tr("Gray")) {
515 qssFile = ":/qss/gray.css";
516 setStyle(QUIWidget::Style_Gray);
517 } else if (name == tr("Light gray")) {
518 qssFile = ":/qss/light gray.css";
519 setStyle(QUIWidget::Style_LightGray);
520 } else if (name == tr("Dark gray")) {
521 qssFile = ":/qss/darkgray.css";
522 setStyle(QUIWidget::Style_DarkGray);
523 } else if (name == tr("Black")) {
524 qssFile = ":/qss/black.css";
525 setStyle(QUIWidget::Style_Black);
526 } else if (name == tr("Light black")) {
527 qssFile = ":/qss/lightblack.css";
528 setStyle(QUIWidget::Style_LightBlack);
529 } else if (name == tr("Dark black")) {
530 qssFile = ":/qss/darkblack.css";
531 setStyle(QUIWidget::Style_DarkBlack);
532 } else if (name == tr("PS black")) {
533 qssFile = ":/qss/psblack.css";
534 setStyle(QUIWidget::Style_PSBlack);
535 } else if (name == tr("Flat black")) {
536 qssFile = ":/qss/flatblack.css";
537 setStyle(QUIWidget::Style_FlatBlack);
538 } else if (name == tr("Flat white")) {
539 qssFile = ":/qss/flatwhite.css";
540 setStyle(QUIWidget::Style_FlatWhite);
541 }
542
543 emit sigChangeStyle(qssFile);
544}
545
546void QUIWidget::setIcon(QUIWidget::Widget widget, QChar str, quint32 size)
547{
548 if (widget == QUIWidget::Lab_Ico) {
549 IconHelper::Instance()->setIcon(this->lab_Ico, str, size);
550 } else if (widget == QUIWidget::BtnMenu) {
551 IconHelper::Instance()->setIcon(this->btnMenu, str, size);
552 } else if (widget == QUIWidget::BtnMenu_Min) {
553 IconHelper::Instance()->setIcon(this->btnMenu_Min, str, size);
554 } else if (widget == QUIWidget::BtnMenu_Max) {
555 IconHelper::Instance()->setIcon(this->btnMenu_Max, str, size);
556 } else if (widget == QUIWidget::BtnMenu_Close) {
557 IconHelper::Instance()->setIcon(this->btnMenu_Close, str, size);
558 }
559}
560
561void QUIWidget::setPixmap(QUIWidget::Widget widget, const QString &file, const QSize &size)
562{
563 QPixmap pix = QPixmap(file);
564 //按照宽高比自动缩放
565 pix = pix.scaled(size, Qt::KeepAspectRatio);
566
567 if (widget == QUIWidget::Lab_Ico) {
568 this->lab_Ico->setPixmap(pix);
569 } else if (widget == QUIWidget::BtnMenu) {
570 this->btnMenu->setIcon(QIcon(file));
571 } else if (widget == QUIWidget::BtnMenu_Min) {
572 this->btnMenu_Min->setIcon(QIcon(file));
573 } else if (widget == QUIWidget::BtnMenu_Max) {
574 this->btnMenu_Max->setIcon(QIcon(file));
575 } else if (widget == QUIWidget::BtnMenu_Close) {
576 this->btnMenu_Close->setIcon(QIcon(file));
577 }
578}
579
580void QUIWidget::setVisible(QUIWidget::Widget widget, bool visible)
581{
582 if (widget == QUIWidget::Lab_Ico) {
583 this->lab_Ico->setVisible(visible);
584 } else if (widget == QUIWidget::BtnMenu) {
585 this->btnMenu->setVisible(visible);
586 } else if (widget == QUIWidget::BtnMenu_Min) {
587 this->btnMenu_Min->setVisible(visible);
588 } else if (widget == QUIWidget::BtnMenu_Max) {
589 this->btnMenu_Max->setVisible(visible);
590 } else if (widget == QUIWidget::BtnMenu_Close) {
591 this->btnMenu_Close->setVisible(visible);
592 }
593}
594
595void QUIWidget::setOnlyCloseBtn()
596{
597 this->btnMenu->setVisible(false);
598 this->btnMenu_Min->setVisible(false);
599 this->btnMenu_Max->setVisible(false);
600}
601
602void QUIWidget::setTitleHeight(int height)
603{
604 this->widget_title->setFixedHeight(height);
605}
606
607void QUIWidget::setBtnWidth(int width)
608{
609 this->lab_Ico->setFixedWidth(width);
610 this->btnMenu->setFixedWidth(width);
611 this->btnMenu_Min->setFixedWidth(width);
612 this->btnMenu_Max->setFixedWidth(width);
613 this->btnMenu_Close->setFixedWidth(width);
614}
615
616void QUIWidget::setTitle(const QString &title)
617{
618 if (this->title != title) {
619 this->title = title;
620 this->lab_Title->setText(title);
621 this->setWindowTitle(this->lab_Title->text());
622 }
623}
624
625void QUIWidget::setAlignment(Qt::Alignment alignment)
626{
627 if (this->alignment != alignment) {
628 this->alignment = alignment;
629 this->lab_Title->setAlignment(alignment);
630 }
631}
632
633void QUIWidget::setMainWidget(QWidget *mainWidget, bool bUsed)
634{
635 Q_ASSERT(mainWidget);
636 //一个QUI窗体对象只能设置一个主窗体
637 if (this->mainWidget == 0) {
638 //将子窗体添加到布局
639 this->widget->layout()->addWidget(mainWidget);
640 //自动设置大小
641 resize(mainWidget->width(), mainWidget->height() + this->widget_title->height());
642
643 this->mainWidget = mainWidget;
644 this->mainWidget->installEventFilter(this);
645
646 if(bUsed)
647 {
648 this->setTitle(mainWidget->windowTitle());
649 QIcon icon = mainWidget->windowIcon();
650 if(!icon.isNull())
651 this->lab_Ico->setPixmap(icon.pixmap(lab_Ico->size()));
652 }
653 } else {
654 Q_ASSERT(0);
655 }
656}
657
658void QUIWidget::on_btnMenu_Min_clicked()
659{
660 showMinimized();
661}
662
663void QUIWidget::on_btnMenu_Max_clicked()
664{
665 if (max) {
666 setIcon(QUIWidget::BtnMenu_Max, QChar(0xf096)); // 0xf067);
667 this->setGeometry(location);
668 } else {
669 setIcon(BtnMenu_Max, QChar(0xf24d));
670 location = this->geometry();
671#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
672 this->setGeometry(qApp->primaryScreen()->availableGeometry());
673#else
674 this->setGeometry(qApp->desktop()->availableGeometry());
675#endif
676 }
677
678 max = !max;
679}
680
681void QUIWidget::on_btnMenu_Close_clicked()
682{
683 bool bClose = false;
684 if(mainWidget)
685 bClose = mainWidget->close();
686 if(bClose)
687 close();
688}
689
690void QUIWidget::resizeEvent(QResizeEvent *event)
691{
692 Q_UNUSED(event)
693 if(isFullScreen())
694 widget_title->setVisible(false);
695 else
696 widget_title->setVisible(true);
697 QDialog::resizeEvent(event);
698}
699
700void QUIWidget::showEvent(QShowEvent *event)
701{
702 Q_UNUSED(event)
703 if(isFullScreen())
704 widget_title->setVisible(false);
705 else
706 widget_title->setVisible(true);
707
708 QDialog::showEvent(event);
709}
710
711QUIMessageBox *QUIMessageBox::self = 0;
712QUIMessageBox::QUIMessageBox(QWidget *parent) : QDialog(parent)
713{
714 this->initControl();
715 this->initForm();
716}
717
718QUIMessageBox::~QUIMessageBox()
719{
720 delete widgetMain;
721}
722
723void QUIMessageBox::initControl()
724{
725 this->setObjectName(QString::fromUtf8("QUIMessageBox"));
726 this->resize(280, 150);
727 verticalLayout1 = new QVBoxLayout(this);
728 verticalLayout1->setSpacing(0);
729 verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1"));
730 verticalLayout1->setContentsMargins(1, 1, 1, 1);
731 widget_title = new QWidget(this);
732 widget_title->setObjectName(QString::fromUtf8("widget_title"));
733 QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
734 sizePolicy.setHorizontalStretch(0);
735 sizePolicy.setVerticalStretch(0);
736 sizePolicy.setHeightForWidth(widget_title->sizePolicy().hasHeightForWidth());
737 widget_title->setSizePolicy(sizePolicy);
738 widget_title->setMinimumSize(QSize(0, 30));
739 horizontalLayout3 = new QHBoxLayout(widget_title);
740 horizontalLayout3->setSpacing(0);
741 horizontalLayout3->setObjectName(QString::fromUtf8("horizontalLayout3"));
742 horizontalLayout3->setContentsMargins(0, 0, 0, 0);
743 lab_Ico = new QLabel(widget_title);
744 lab_Ico->setObjectName(QString::fromUtf8("lab_Ico"));
745 QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred);
746 sizePolicy1.setHorizontalStretch(0);
747 sizePolicy1.setVerticalStretch(0);
748 sizePolicy1.setHeightForWidth(lab_Ico->sizePolicy().hasHeightForWidth());
749 lab_Ico->setSizePolicy(sizePolicy1);
750 lab_Ico->setMinimumSize(QSize(31, 0));
751 lab_Ico->setAlignment(Qt::AlignCenter);
752
753 horizontalLayout3->addWidget(lab_Ico);
754
755 lab_Title = new QLabel(widget_title);
756 lab_Title->setObjectName(QString::fromUtf8("lab_Title"));
757 lab_Title->setStyleSheet(QString::fromUtf8(""));
758 lab_Title->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
759
760 horizontalLayout3->addWidget(lab_Title);
761
762 labTime = new QLabel(widget_title);
763 labTime->setObjectName(QString::fromUtf8("labTime"));
764 QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred);
765 sizePolicy2.setHorizontalStretch(0);
766 sizePolicy2.setVerticalStretch(0);
767 sizePolicy2.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth());
768 labTime->setSizePolicy(sizePolicy2);
769 labTime->setAlignment(Qt::AlignCenter);
770
771 horizontalLayout3->addWidget(labTime);
772
773 widget_menu = new QWidget(widget_title);
774 widget_menu->setObjectName(QString::fromUtf8("widget_menu"));
775 sizePolicy1.setHeightForWidth(widget_menu->sizePolicy().hasHeightForWidth());
776 widget_menu->setSizePolicy(sizePolicy1);
777 horizontalLayout4 = new QHBoxLayout(widget_menu);
778 horizontalLayout4->setSpacing(0);
779 horizontalLayout4->setObjectName(QString::fromUtf8("horizontalLayout4"));
780 horizontalLayout4->setContentsMargins(0, 0, 0, 0);
781 btnMenu_Close = new QPushButton(widget_menu);
782 btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close"));
783 QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding);
784 sizePolicy3.setHorizontalStretch(0);
785 sizePolicy3.setVerticalStretch(0);
786 sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth());
787 btnMenu_Close->setSizePolicy(sizePolicy3);
788 btnMenu_Close->setMinimumSize(QSize(30, 0));
789 btnMenu_Close->setMaximumSize(QSize(30, 16777215));
790 btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor));
791 btnMenu_Close->setFocusPolicy(Qt::NoFocus);
792 btnMenu_Close->setFlat(true);
793
794 horizontalLayout4->addWidget(btnMenu_Close);
795 horizontalLayout3->addWidget(widget_menu);
796 verticalLayout1->addWidget(widget_title);
797
798 widgetMain = new QWidget(this);
799 widgetMain->setObjectName(QString::fromUtf8("widgetMain"));
800 widgetMain->setStyleSheet(QString::fromUtf8(""));
801 verticalLayout2 = new QVBoxLayout(widgetMain);
802 verticalLayout2->setSpacing(5);
803 verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2"));
804 verticalLayout2->setContentsMargins(5, 5, 5, 5);
805 frame = new QFrame(widgetMain);
806 frame->setObjectName(QString::fromUtf8("frame"));
807 frame->setFrameShape(QFrame::Box);
808 frame->setFrameShadow(QFrame::Sunken);
809 verticalLayout4 = new QVBoxLayout(frame);
810 verticalLayout4->setObjectName(QString::fromUtf8("verticalLayout4"));
811 verticalLayout4->setContentsMargins(-1, 9, -1, -1);
812 horizontalLayout1 = new QHBoxLayout();
813 horizontalLayout1->setObjectName(QString::fromUtf8("horizontalLayout1"));
814 labIcoMain = new QLabel(frame);
815 labIcoMain->setObjectName(QString::fromUtf8("labIcoMain"));
816 labIcoMain->setMinimumSize(QSize(30, 30));
817 labIcoMain->setMaximumSize(QSize(30, 30));
818 labIcoMain->setStyleSheet(QString::fromUtf8("border-image: url(:/image/msg_info.png);"));
819
820 horizontalLayout1->addWidget(labIcoMain);
821
822 horizontalSpacer1 = new QSpacerItem(5, 0, QSizePolicy::Minimum, QSizePolicy::Minimum);
823
824 horizontalLayout1->addItem(horizontalSpacer1);
825
826 labInfo = new QLabel(frame);
827 labInfo->setObjectName(QString::fromUtf8("labInfo"));
828 QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Expanding);
829 sizePolicy4.setHorizontalStretch(0);
830 sizePolicy4.setVerticalStretch(0);
831 sizePolicy4.setHeightForWidth(labInfo->sizePolicy().hasHeightForWidth());
832 labInfo->setSizePolicy(sizePolicy4);
833 labInfo->setMinimumSize(QSize(0, 33));
834 labInfo->setScaledContents(false);
835 labInfo->setWordWrap(true);
836
837 horizontalLayout1->addWidget(labInfo);
838
839
840 verticalLayout4->addLayout(horizontalLayout1);
841
842 horizontalLayout2 = new QHBoxLayout();
843 horizontalLayout2->setObjectName(QString::fromUtf8("horizontalLayout2"));
844 horizontalSpacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
845
846 horizontalLayout2->addItem(horizontalSpacer2);
847
848 btnOk = new QPushButton(frame);
849 btnOk->setObjectName(QString::fromUtf8("btnOk"));
850 btnOk->setMinimumSize(QSize(80, 0));
851 btnOk->setFocusPolicy(Qt::StrongFocus);
852 btnOk->setStyleSheet(QString::fromUtf8(""));
853 QIcon icon;
854 icon.addFile(QString::fromUtf8(":/image/btn_ok.png"), QSize(), QIcon::Normal, QIcon::Off);
855 btnOk->setIcon(icon);
856
857 horizontalLayout2->addWidget(btnOk);
858
859 btnCancel = new QPushButton(frame);
860 btnCancel->setObjectName(QString::fromUtf8("btnCancel"));
861 btnCancel->setMinimumSize(QSize(80, 0));
862 btnCancel->setFocusPolicy(Qt::StrongFocus);
863 btnCancel->setStyleSheet(QString::fromUtf8(""));
864 QIcon icon1;
865 icon1.addFile(QString::fromUtf8(":/image/btn_close.png"), QSize(), QIcon::Normal, QIcon::Off);
866 btnCancel->setIcon(icon1);
867
868 horizontalLayout2->addWidget(btnCancel);
869 verticalLayout4->addLayout(horizontalLayout2);
870 verticalLayout2->addWidget(frame);
871 verticalLayout1->addWidget(widgetMain);
872
873 widget_title->raise();
874 widgetMain->raise();
875 frame->raise();
876
877 this->btnOk->setText(tr("OK"));
878 this->btnCancel->setText(tr("Cancel"));
879
880 connect(this->btnOk, SIGNAL(clicked()), this, SLOT(on_btnOk_clicked()));
881 connect(this->btnMenu_Close, SIGNAL(clicked(bool)), this, SLOT(on_btnMenu_Close_clicked()));
882 connect(this->btnCancel, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked()));
883}
884
885void QUIMessageBox::initForm()
886{
887 IconHelper::Instance()->setIcon(this->lab_Ico, QChar(0xf072), 11);
888 IconHelper::Instance()->setIcon(this->btnMenu_Close, QChar(0xf00d), 9);
889
890 this->setProperty("form", true);
891 this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint);
892 this->setFixedSize(280, 150);
893 this->setWindowTitle(this->lab_Title->text());
894
895 int width = 80;
896 int iconWidth = 18;
897 int iconHeight = 18;
898 this->labIcoMain->setFixedSize(30, 30);
899
900 QList<QPushButton *> btns1 = this->frame->findChildren<QPushButton *>();
901
902 foreach (QPushButton *btn, btns1) {
903 btn->setMinimumWidth(width);
904 btn->setIconSize(QSize(iconWidth, iconHeight));
905 }
906
907 closeSec = 0;
908 currentSec = 0;
909
910 QTimer *timer = new QTimer(this);
911 timer->setInterval(1000);
912 connect(timer, SIGNAL(timeout()), this, SLOT(checkSec()));
913 timer->start();
914
915 this->installEventFilter(this);
916}
917
918void QUIMessageBox::checkSec()
919{
920 if (closeSec == 0) {
921 return;
922 }
923
924 if (currentSec < closeSec) {
925 currentSec++;
926 } else {
927 this->close();
928 }
929
930 QString str = tr("Turn off countdown %1 s").arg(closeSec - currentSec + 1);
931 this->labTime->setText(str);
932}
933
934void QUIMessageBox::setMessage(const QString &msg, int type, int closeSec)
935{
936 this->closeSec = closeSec;
937 this->currentSec = 0;
938 this->labTime->clear();
939
940 checkSec();
941
942 if (type == 0) {
943 this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_info.png);");
944 this->btnCancel->setVisible(false);
945 this->lab_Title->setText(tr("Prompt"));
946 } else if (type == 1) {
947 this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_question.png);");
948 this->lab_Title->setText(tr("Query"));
949 } else if (type == 2) {
950 this->labIcoMain->setStyleSheet("border-image: url(:/image/msg_error.png);");
951 this->btnCancel->setVisible(false);
952 this->lab_Title->setText(tr("Error"));
953 }
954
955 this->labInfo->setText(msg);
956 this->setWindowTitle(this->lab_Title->text());
957}
958
959void QUIMessageBox::closeEvent(QCloseEvent *)
960{
961 closeSec = 0;
962 currentSec = 0;
963}
964
965bool QUIMessageBox::eventFilter(QObject *obj, QEvent *evt)
966{
967 static QPoint mousePoint;
968 static bool mousePressed = false;
969
970 QMouseEvent *event = static_cast<QMouseEvent *>(evt);
971 if (event->type() == QEvent::MouseButtonPress) {
972 if (event->button() == Qt::LeftButton) {
973 mousePressed = true;
974 mousePoint = event->globalPos() - this->pos();
975 return true;
976 }
977 } else if (event->type() == QEvent::MouseButtonRelease) {
978 mousePressed = false;
979 return true;
980 } else if (event->type() == QEvent::MouseMove) {
981 if (mousePressed && (event->buttons() && Qt::LeftButton)) {
982 this->move(event->globalPos() - mousePoint);
983 return true;
984 }
985 }
986
987 return QWidget::eventFilter(obj, evt);
988}
989
990void QUIMessageBox::on_btnOk_clicked()
991{
992 done(1);
993 close();
994}
995
996void QUIMessageBox::on_btnMenu_Close_clicked()
997{
998 close();
999}
1000
1001
1002
1003QUIInputBox *QUIInputBox::self = 0;
1004QUIInputBox::QUIInputBox(QWidget *parent) : QDialog(parent)
1005{
1006 this->initControl();
1007 this->initForm();
1008}
1009
1010QUIInputBox::~QUIInputBox()
1011{
1012 delete widgetMain;
1013}
1014
1015void QUIInputBox::initControl()
1016{
1017 this->setObjectName(QString::fromUtf8("QUIInputBox"));
1018 this->resize(260, 166);
1019 verticalLayout1 = new QVBoxLayout(this);
1020 verticalLayout1->setSpacing(0);
1021 verticalLayout1->setObjectName(QString::fromUtf8("verticalLayout1"));
1022 verticalLayout1->setContentsMargins(1, 1, 1, 1);
1023 widget_title = new QWidget(this);
1024 widget_title->setObjectName(QString::fromUtf8("widget_title"));
1025 QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
1026 sizePolicy.setHorizontalStretch(0);
1027 sizePolicy.setVerticalStretch(0);
1028 sizePolicy.setHeightForWidth(widget_title->sizePolicy().hasHeightForWidth());
1029 widget_title->setSizePolicy(sizePolicy);
1030 widget_title->setMinimumSize(QSize(0, 30));
1031 horizontalLayout1 = new QHBoxLayout(widget_title);
1032 horizontalLayout1->setSpacing(0);
1033 horizontalLayout1->setObjectName(QString::fromUtf8("horizontalLayout1"));
1034 horizontalLayout1->setContentsMargins(0, 0, 0, 0);
1035 lab_Ico = new QLabel(widget_title);
1036 lab_Ico->setObjectName(QString::fromUtf8("lab_Ico"));
1037 QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred);
1038 sizePolicy1.setHorizontalStretch(0);
1039 sizePolicy1.setVerticalStretch(0);
1040 sizePolicy1.setHeightForWidth(lab_Ico->sizePolicy().hasHeightForWidth());
1041 lab_Ico->setSizePolicy(sizePolicy1);
1042 lab_Ico->setMinimumSize(QSize(31, 0));
1043 lab_Ico->setAlignment(Qt::AlignCenter);
1044
1045 horizontalLayout1->addWidget(lab_Ico);
1046
1047 lab_Title = new QLabel(widget_title);
1048 lab_Title->setObjectName(QString::fromUtf8("lab_Title"));
1049 lab_Title->setStyleSheet(QString::fromUtf8(""));
1050 lab_Title->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
1051
1052 horizontalLayout1->addWidget(lab_Title);
1053
1054 labTime = new QLabel(widget_title);
1055 labTime->setObjectName(QString::fromUtf8("labTime"));
1056 QSizePolicy sizePolicy2(QSizePolicy::Expanding, QSizePolicy::Preferred);
1057 sizePolicy2.setHorizontalStretch(0);
1058 sizePolicy2.setVerticalStretch(0);
1059 sizePolicy2.setHeightForWidth(labTime->sizePolicy().hasHeightForWidth());
1060 labTime->setSizePolicy(sizePolicy2);
1061 labTime->setAlignment(Qt::AlignCenter);
1062
1063 horizontalLayout1->addWidget(labTime);
1064
1065 widget_menu = new QWidget(widget_title);
1066 widget_menu->setObjectName(QString::fromUtf8("widget_menu"));
1067 sizePolicy1.setHeightForWidth(widget_menu->sizePolicy().hasHeightForWidth());
1068 widget_menu->setSizePolicy(sizePolicy1);
1069 horizontalLayout2 = new QHBoxLayout(widget_menu);
1070 horizontalLayout2->setSpacing(0);
1071 horizontalLayout2->setObjectName(QString::fromUtf8("horizontalLayout2"));
1072 horizontalLayout2->setContentsMargins(0, 0, 0, 0);
1073 btnMenu_Close = new QPushButton(widget_menu);
1074 btnMenu_Close->setObjectName(QString::fromUtf8("btnMenu_Close"));
1075 QSizePolicy sizePolicy3(QSizePolicy::Minimum, QSizePolicy::Expanding);
1076 sizePolicy3.setHorizontalStretch(0);
1077 sizePolicy3.setVerticalStretch(0);
1078 sizePolicy3.setHeightForWidth(btnMenu_Close->sizePolicy().hasHeightForWidth());
1079 btnMenu_Close->setSizePolicy(sizePolicy3);
1080 btnMenu_Close->setMinimumSize(QSize(30, 0));
1081 btnMenu_Close->setMaximumSize(QSize(30, 16777215));
1082 btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor));
1083 btnMenu_Close->setFocusPolicy(Qt::NoFocus);
1084 btnMenu_Close->setFlat(true);
1085
1086 horizontalLayout2->addWidget(btnMenu_Close);
1087 horizontalLayout1->addWidget(widget_menu);
1088 verticalLayout1->addWidget(widget_title);
1089
1090 widgetMain = new QWidget(this);
1091 widgetMain->setObjectName(QString::fromUtf8("widgetMain"));
1092 widgetMain->setStyleSheet(QString::fromUtf8(""));
1093 verticalLayout2 = new QVBoxLayout(widgetMain);
1094 verticalLayout2->setSpacing(5);
1095 verticalLayout2->setObjectName(QString::fromUtf8("verticalLayout2"));
1096 verticalLayout2->setContentsMargins(5, 5, 5, 5);
1097 frame = new QFrame(widgetMain);
1098 frame->setObjectName(QString::fromUtf8("frame"));
1099 frame->setFrameShape(QFrame::Box);
1100 frame->setFrameShadow(QFrame::Sunken);
1101 verticalLayout3 = new QVBoxLayout(frame);
1102 verticalLayout3->setObjectName(QString::fromUtf8("verticalLayout3"));
1103 labInfo = new QLabel(frame);
1104 labInfo->setObjectName(QString::fromUtf8("labInfo"));
1105 labInfo->setScaledContents(false);
1106 labInfo->setWordWrap(true);
1107
1108 verticalLayout3->addWidget(labInfo);
1109
1110 txtValue = new QLineEdit(frame);
1111 txtValue->setObjectName(QString::fromUtf8("txtValue"));
1112
1113 verticalLayout3->addWidget(txtValue);
1114
1115 cboxValue = new QComboBox(frame);
1116 cboxValue->setObjectName(QString::fromUtf8("cboxValue"));
1117
1118 verticalLayout3->addWidget(cboxValue);
1119
1120 lay = new QHBoxLayout();
1121 lay->setObjectName(QString::fromUtf8("lay"));
1122 horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
1123
1124 lay->addItem(horizontalSpacer);
1125
1126 btnOk = new QPushButton(frame);
1127 btnOk->setObjectName(QString::fromUtf8("btnOk"));
1128 btnOk->setMinimumSize(QSize(80, 0));
1129 btnOk->setStyleSheet(QString::fromUtf8(""));
1130 QIcon icon;
1131 icon.addFile(QString::fromUtf8(":/image/btn_ok.png"), QSize(), QIcon::Normal, QIcon::Off);
1132 btnOk->setIcon(icon);
1133
1134 lay->addWidget(btnOk);
1135
1136 btnCancel = new QPushButton(frame);
1137 btnCancel->setObjectName(QString::fromUtf8("btnCancel"));
1138 btnCancel->setMinimumSize(QSize(80, 0));
1139 btnCancel->setStyleSheet(QString::fromUtf8(""));
1140 QIcon icon1;
1141 icon1.addFile(QString::fromUtf8(":/image/btn_close.png"), QSize(), QIcon::Normal, QIcon::Off);
1142 btnCancel->setIcon(icon1);
1143
1144 lay->addWidget(btnCancel);
1145 verticalLayout3->addLayout(lay);
1146 verticalLayout2->addWidget(frame);
1147 verticalLayout1->addWidget(widgetMain);
1148
1149 setTabOrder(txtValue, btnOk);
1150 setTabOrder(btnOk, btnCancel);
1151
1152 this->lab_Title->setText(tr("Input box"));
1153 this->btnOk->setText(tr("OK"));
1154 this->btnCancel->setText(tr("Cancel"));
1155
1156 connect(this->btnOk, SIGNAL(clicked()), this, SLOT(on_btnOk_clicked()));
1157 connect(this->btnMenu_Close, SIGNAL(clicked(bool)), this, SLOT(on_btnMenu_Close_clicked()));
1158 connect(this->btnCancel, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked()));
1159}
1160
1161void QUIInputBox::initForm()
1162{
1163 //设置图形字体作为图标
1164 IconHelper::Instance()->setIcon(this->lab_Ico, QChar(0xf072), 11);
1165 IconHelper::Instance()->setIcon(this->btnMenu_Close, QChar(0xf00d), 9);
1166
1167 this->setProperty("form", true);
1168 this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint);
1169 this->setFixedSize(280, 150);
1170 this->setWindowTitle(this->lab_Title->text());
1171
1172 int width = 80;
1173 int iconWidth = 18;
1174 int iconHeight = 18;
1175
1176 QList<QPushButton *> btns = this->frame->findChildren<QPushButton *>();
1177
1178 foreach (QPushButton *btn, btns) {
1179 btn->setMinimumWidth(width);
1180 btn->setIconSize(QSize(iconWidth, iconHeight));
1181 }
1182
1183 closeSec = 0;
1184 currentSec = 0;
1185
1186 QTimer *timer = new QTimer(this);
1187 timer->setInterval(1000);
1188 connect(timer, SIGNAL(timeout()), this, SLOT(checkSec()));
1189 timer->start();
1190
1191 this->installEventFilter(this);
1192}
1193
1194void QUIInputBox::checkSec()
1195{
1196 if (closeSec == 0) {
1197 return;
1198 }
1199
1200 if (currentSec < closeSec) {
1201 currentSec++;
1202 } else {
1203 this->close();
1204 }
1205
1206 QString str = tr("Turn off countdown %1 s").arg(closeSec - currentSec + 1);
1207 this->labTime->setText(str);
1208}
1209
1210void QUIInputBox::setParameter(const QString &title, int type, int closeSec,
1211 QString defaultValue, bool pwd)
1212{
1213 this->closeSec = closeSec;
1214 this->currentSec = 0;
1215 this->labTime->clear();
1216 this->labInfo->setText(title);
1217
1218 checkSec();
1219
1220 if (type == 0) {
1221 this->cboxValue->setVisible(false);
1222 this->txtValue->setPlaceholderText(defaultValue);
1223
1224 if (pwd) {
1225 this->txtValue->setEchoMode(QLineEdit::Password);
1226 }
1227 } else if (type == 1) {
1228 this->txtValue->setVisible(false);
1229 this->cboxValue->addItems(defaultValue.split("|"));
1230 }
1231}
1232
1233QString QUIInputBox::getValue() const
1234{
1235 return this->value;
1236}
1237
1238void QUIInputBox::closeEvent(QCloseEvent *)
1239{
1240 closeSec = 0;
1241 currentSec = 0;
1242}
1243
1244bool QUIInputBox::eventFilter(QObject *obj, QEvent *evt)
1245{
1246 static QPoint mousePoint;
1247 static bool mousePressed = false;
1248
1249 QMouseEvent *event = static_cast<QMouseEvent *>(evt);
1250 if (event->type() == QEvent::MouseButtonPress) {
1251 if (event->button() == Qt::LeftButton) {
1252 mousePressed = true;
1253 mousePoint = event->globalPos() - this->pos();
1254 return true;
1255 }
1256 } else if (event->type() == QEvent::MouseButtonRelease) {
1257 mousePressed = false;
1258 return true;
1259 } else if (event->type() == QEvent::MouseMove) {
1260 if (mousePressed && (event->buttons() && Qt::LeftButton)) {
1261 this->move(event->globalPos() - mousePoint);
1262 return true;
1263 }
1264 }
1265
1266 return QWidget::eventFilter(obj, evt);
1267}
1268
1269void QUIInputBox::on_btnOk_clicked()
1270{
1271 if (this->txtValue->isVisible()) {
1272 value = this->txtValue->text();
1273 } else if (this->cboxValue->isVisible()) {
1274 value = this->cboxValue->currentText();
1275 }
1276
1277 done(1);
1278 close();
1279}
1280
1281void QUIInputBox::on_btnMenu_Close_clicked()
1282{
1283 close();
1284}
1285
1286
1287IconHelper *IconHelper::self = 0;
1288IconHelper::IconHelper(QObject *) : QObject(qApp)
1289{
1290 int fontId = QFontDatabase::addApplicationFont(":/image/fontawesome-webfont.ttf");
1291 QStringList fontName = QFontDatabase::applicationFontFamilies(fontId);
1292
1293 if (fontName.count() > 0) {
1294 iconFont = QFont(fontName.at(0));
1295 } else {
1296 qDebug() << "load fontawesome-webfont.ttf error";
1297 }
1298
1299}
1300
1301void IconHelper::setIcon(QLabel *lab, QChar c, quint32 size)
1302{
1303 iconFont.setPointSize(size);
1304 lab->setFont(iconFont);
1305 lab->setText(c);
1306}
1307
1308void IconHelper::setIcon(QAbstractButton *btn, QChar c, quint32 size)
1309{
1310 iconFont.setPointSize(size);
1311 btn->setFont(iconFont);
1312 btn->setText(c);
1313}