quiwidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #pragma execution_character_set("utf-8")
  2. #include "quiwidget.h"
  3. #include <QtWidgets>
  4. #include "WindowMain.h"
  5. #include "WindowAppMdiFrame.h"
  6. #include "TaskManager.h"
  7. #include "WindowAppTaskMonitorView.h"
  8. #include "Preferences.h"
  9. #define TOOLBAR_WIDTH 30
  10. QUIWidget::QUIWidget(QWidget* widget, QWidget *parent) :
  11. QDialog(parent)
  12. , mainWidget(widget)
  13. {
  14. Init();
  15. // setMainWidget(widget);
  16. }
  17. QUIWidget::~QUIWidget()
  18. {
  19. }
  20. void QUIWidget::Init()
  21. {
  22. this->initControl();
  23. this->initForm();
  24. }
  25. bool QUIWidget::eventFilter(QObject* obj, QEvent* evt)
  26. {
  27. static QPoint mousePoint;
  28. static bool mousePressed = false;
  29. QMouseEvent* event = static_cast<QMouseEvent*>(evt);
  30. if (event->type() == QEvent::MouseButtonPress)
  31. {
  32. if (event->button() == Qt::LeftButton)
  33. {
  34. mousePressed = true;
  35. mousePoint = event->globalPos() - this->pos();
  36. }
  37. }
  38. else if (event->type() == QEvent::MouseButtonRelease)
  39. {
  40. mousePressed = false;
  41. }
  42. else if (event->type() == QEvent::MouseMove)
  43. {
  44. if (mousePressed && (event->buttons() && Qt::LeftButton))
  45. {
  46. if (this->property("canMove").toBool())
  47. {
  48. this->move(event->globalPos() - mousePoint);
  49. }
  50. }
  51. }
  52. else if (event->type() == QEvent::MouseButtonDblClick)
  53. {
  54. if (this->btnMenu_Max->isVisible() && obj == this->widgetTitle)
  55. {
  56. this->on_btnMenu_Max_clicked();
  57. }
  58. }
  59. if (event->type() == QEvent::KeyPress)
  60. {
  61. return true;
  62. }
  63. return QWidget::eventFilter(obj, evt);
  64. }
  65. QSize QUIWidget::sizeHint() const
  66. {
  67. return QSize(600, 450);
  68. }
  69. QSize QUIWidget::minimumSizeHint() const
  70. {
  71. return QSize(200, 10);
  72. }
  73. void QUIWidget::initControl()
  74. {
  75. // WindowMain* pWindowMain = new WindowMain;
  76. WindowMain* pWindowMain = (WindowMain*)mainWidget;
  77. verticalLayout1 = new QVBoxLayout(this);
  78. verticalLayout1->setSpacing(0);
  79. verticalLayout1->setContentsMargins(0, 0, 0, 0);
  80. widgetMain = new QWidget(this);
  81. verticalLayout2 = new QVBoxLayout(widgetMain);
  82. verticalLayout2->setSpacing(0);
  83. verticalLayout2->setContentsMargins(0, 0, 0, 0);
  84. widgetTitle = new QWidget(widgetMain);
  85. widgetTitle->setMinimumSize(QSize(0, TOOLBAR_WIDTH));
  86. widgetTitle->setStyleSheet("background-color:rgb(9, 101, 90);border:0px;");
  87. horizontalLayout4 = new QHBoxLayout(widgetTitle);
  88. horizontalLayout4->setSpacing(5);
  89. horizontalLayout4->setContentsMargins(30, 0, 0, 0);
  90. //文件按钮
  91. File = new QPushButton(widgetTitle);
  92. File->setText(" 文件");
  93. File->setMinimumSize(QSize(0, TOOLBAR_WIDTH));
  94. File->setStyleSheet("QPushButton{color:rgb(255, 255, 255);font:10pt'微软雅黑';}QPushButton::pressed{background-color:rgb(34, 136, 124);}QPushButton::hover{background-color:rgb(34, 136, 124);}QPushButton::menu-indicator{image:none;}QPushButton{ text - align:center; }");
  95. QMenu* menufile = new QMenu(this);
  96. QAction* NewAction = new QAction("新建", menufile);
  97. QAction* OpenAction = new QAction("打开...", menufile);
  98. QAction* CurOpenAction = new QAction("最近打开...", menufile);
  99. QAction* SaveAction = new QAction("保存...", menufile);
  100. QAction* SaveAsAction = new QAction("另存为...", menufile);
  101. QAction* ExitAction = new QAction("退出程序", menufile);
  102. menufile->addAction(NewAction);
  103. menufile->addAction(OpenAction);
  104. menufile->addAction(CurOpenAction);
  105. menufile->addAction(SaveAction);
  106. menufile->addAction(SaveAsAction);
  107. menufile->addSeparator();
  108. menufile->addAction(ExitAction);
  109. connect(NewAction, &QAction::triggered, pWindowMain, &WindowMain::onFileNew);
  110. connect(OpenAction, &QAction::triggered, pWindowMain, &WindowMain::onFileOpen);
  111. connect(SaveAction, &QAction::triggered, pWindowMain, &WindowMain::onFileSave);
  112. connect(SaveAsAction, &QAction::triggered, pWindowMain, &WindowMain::onFileSaveAs);
  113. //connect(ExitAction, &QAction::triggered, pWindowMain, &WindowMain::close);
  114. connect(ExitAction, &QAction::triggered, this, &QUIWidget::on_btnMenu_Close_clicked);
  115. File->setMenu(menufile);
  116. menufile->setStyleSheet("QMenu::item{padding-left:40px;height:35px;width:200px;}QMenu{background-color:rgb(9, 101, 90);color:white;font:9pt'微软雅黑';margin:0px 0px 0px 0px;}QMenu::item:selected{background-color:rgba(255, 255, 255,50);}");
  117. //视图
  118. View = new QPushButton(widgetTitle);
  119. View->setText(" 视图");
  120. View->setMinimumSize(QSize(0, TOOLBAR_WIDTH));
  121. View->setStyleSheet("QPushButton{color:rgb(255, 255, 255);font:12pt'微软雅黑';}QPushButton::pressed{background-color:rgb(34, 136, 124);}QPushButton::hover{background-color:rgb(34, 136, 124);}QPushButton::menu-indicator{image:none;}");
  122. QMenu* menuView = new QMenu(this);
  123. QAction* RuntimeAction = new QAction("运行视图", menuView);
  124. QAction* AppAction = new QAction("程序视图", menuView);
  125. menuView->addAction(RuntimeAction);
  126. menuView->addAction(AppAction);
  127. connect(RuntimeAction, &QAction::triggered, pWindowMain, &WindowMain::onViewRunTime);
  128. connect(AppAction, &QAction::triggered, pWindowMain, &WindowMain::onViewApp);
  129. View->setMenu(menuView);
  130. menuView->setStyleSheet("QMenu::item{padding-left:40px;height:35px;width:200px;}QMenu{background-color:rgb(9, 101, 90);color:white;font:9pt'微软雅黑';margin:0px 0px 0px 0px;}QMenu::item:selected{background-color:rgba(255, 255, 255,50);}");
  131. //任务
  132. Task = new QPushButton(widgetTitle);
  133. Task->setText(" 任务");
  134. Task->setMinimumSize(QSize(0, TOOLBAR_WIDTH));
  135. Task->setStyleSheet("QPushButton{color:rgb(255, 255, 255);font:12pt'微软雅黑';}QPushButton::pressed{background-color:rgb(34, 136, 124);}QPushButton::hover{background-color:rgb(34, 136, 124);}QPushButton::menu-indicator{image:none;}");
  136. QMenu* menuTask = new QMenu(this);
  137. QAction* SysRunAction = new QAction("循环运行", menuTask);
  138. QAction* SysRunOnceAction = new QAction("单步运行", menuTask);
  139. QAction* SysStopAction = new QAction("停止运行", menuTask);
  140. menuTask->addAction(SysRunAction);
  141. menuTask->addAction(SysRunOnceAction);
  142. menuTask->addAction(SysStopAction);
  143. connect(SysRunAction, &QAction::triggered, pWindowMain, &WindowMain::onTaskRun);
  144. connect(SysRunOnceAction, &QAction::triggered, pWindowMain, &WindowMain::onTaskRunonce);
  145. connect(SysStopAction, &QAction::triggered, pWindowMain, &WindowMain::onTaskStop);
  146. Task->setMenu(menuTask);
  147. menuTask->setStyleSheet("QMenu::item{padding-left:40px;height:35px;width:200px;}QMenu{background-color:rgb(9, 101, 90);color:white;font:9pt'微软雅黑';margin:0px 0px 0px 0px;}QMenu::item:selected{background-color:rgba(255, 255, 255,50);}");
  148. //系统
  149. System = new QPushButton(widgetTitle);
  150. System->setText(" 系统");
  151. System->setMinimumSize(QSize(0, TOOLBAR_WIDTH));
  152. System->setStyleSheet("QPushButton{color:rgb(255, 255, 255);font:12pt'微软雅黑';}QPushButton::pressed{background-color:rgb(34, 136, 124);}QPushButton::hover{background-color:rgb(34, 136, 124);}QPushButton::menu-indicator{image:none;}");
  153. QMenu* menuSys = new QMenu(this);
  154. QAction* PreferencesAction = new QAction("偏好设置", menuSys);
  155. QAction* SysInfoAction = new QAction("系统信息", menuSys);
  156. menuSys->addAction(PreferencesAction);
  157. menuSys->addAction(SysInfoAction);
  158. connect(PreferencesAction, &QAction::triggered, pWindowMain, &WindowMain::onPreferences);
  159. connect(SysInfoAction, &QAction::triggered, pWindowMain, &WindowMain::onAppInfo);
  160. System->setMenu(menuSys);
  161. menuSys->setStyleSheet("QMenu::item{padding-left:40px;height:35px;width:200px;}QMenu{background-color:rgb(9, 101, 90);color:white;font:9pt'微软雅黑';margin:0px 0px 0px 0px;}QMenu::item:selected{background-color:rgba(255, 255, 255,50);}");
  162. QLabel* text = new QLabel(widgetTitle);
  163. text->setText(thePrefs.m_strProductName);
  164. text->setStyleSheet("color:rgb(255, 255, 255);font:10pt'微软雅黑';");
  165. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  166. File->setSizePolicy(sizePolicy);
  167. horizontalLayout4->addWidget(File);
  168. horizontalLayout4->addWidget(View);
  169. horizontalLayout4->addWidget(Task);
  170. horizontalLayout4->addWidget(System);
  171. horizontalLayout4->addWidget(text);
  172. horizontalLayout4->addStretch(1);
  173. widgetMenu = new QWidget(widgetTitle);
  174. horizontalLayout = new QHBoxLayout(widgetMenu);
  175. horizontalLayout->setSpacing(0);
  176. horizontalLayout->setContentsMargins(0, 0, 0, 0);
  177. //最小化按钮
  178. btnMenu_Min = new QPushButton(widgetMenu);
  179. btnMenu_Min->setMinimumSize(QSize(30, 0));
  180. btnMenu_Min->setMaximumSize(QSize(30, 16777215));
  181. btnMenu_Min->setCursor(QCursor(Qt::ArrowCursor));
  182. btnMenu_Min->setFocusPolicy(Qt::NoFocus);
  183. horizontalLayout->addWidget(btnMenu_Min);
  184. btnMenu_Min->setIcon(QIcon(":/image/min.png"));
  185. btnMenu_Min->setStyleSheet("background:transparent;");
  186. //最大化按钮
  187. btnMenu_Max = new QPushButton(widgetMenu);
  188. btnMenu_Max->setMinimumSize(QSize(30, 0));
  189. btnMenu_Max->setMaximumSize(QSize(30, 16777215));
  190. btnMenu_Max->setCursor(QCursor(Qt::ArrowCursor));
  191. btnMenu_Max->setFocusPolicy(Qt::NoFocus);
  192. horizontalLayout->addWidget(btnMenu_Max);
  193. btnMenu_Max->setIcon(QIcon(":/image/max.png"));
  194. btnMenu_Max->setStyleSheet("background:transparent;");
  195. //关闭按钮
  196. btnMenu_Close = new QPushButton(widgetMenu);
  197. btnMenu_Close->setMinimumSize(QSize(30, 0));
  198. btnMenu_Close->setMaximumSize(QSize(30, 16777215));
  199. btnMenu_Close->setCursor(QCursor(Qt::ArrowCursor));
  200. btnMenu_Close->setFocusPolicy(Qt::NoFocus);
  201. horizontalLayout->addWidget(btnMenu_Close);
  202. btnMenu_Close->setIcon(QIcon(":/image/close.png"));
  203. btnMenu_Close->setStyleSheet("background:transparent;");
  204. horizontalLayout4->addWidget(widgetMenu);
  205. verticalLayout2->addWidget(widgetTitle);
  206. widget = new QWidget(widgetMain);
  207. verticalLayout3 = new QVBoxLayout(widget);
  208. verticalLayout3->setSpacing(0);
  209. verticalLayout3->setContentsMargins(0, 0, 0, 0);
  210. verticalLayout2->addWidget(widget);
  211. verticalLayout1->addWidget(widgetMain);
  212. connect(this->btnMenu_Min, SIGNAL(clicked()), this, SLOT(on_btnMenu_Min_clicked()));
  213. connect(this->btnMenu_Max, SIGNAL(clicked()), this, SLOT(on_btnMenu_Max_clicked()));
  214. connect(this->btnMenu_Close, SIGNAL(clicked()), this, SLOT(on_btnMenu_Close_clicked()));
  215. }
  216. void QUIWidget::triggeredSlot(bool ok)
  217. {
  218. Q_UNUSED(ok);
  219. // QAction* action = (QAction*)sender();
  220. }
  221. void QUIWidget::initForm()
  222. {
  223. this->setProperty("form", true);
  224. this->setProperty("canMove", true);
  225. this->widgetTitle->setProperty("form", "title");
  226. this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
  227. minHide = false;
  228. // mainWidget = 0;
  229. this->widget->layout()->addWidget(mainWidget);
  230. resize(mainWidget->width(), mainWidget->height() + this->widgetTitle->height());
  231. this->installEventFilter(this);
  232. this->widgetTitle->installEventFilter(this);
  233. }
  234. //void QUIWidget::setMainWidget(QWidget* mainWidget)
  235. //{
  236. // //if (this->mainWidget == 0)
  237. // //{
  238. // //this->widget->layout()->addWidget(mainWidget);
  239. // //resize(mainWidget->width(), mainWidget->height() + this->widgetTitle->height());
  240. // // this->mainWidget = mainWidget;
  241. // // }
  242. //}
  243. void QUIWidget::on_btnMenu_Min_clicked()
  244. {
  245. if (minHide)
  246. {
  247. hide();
  248. }
  249. else
  250. {
  251. showMinimized();
  252. }
  253. }
  254. void QUIWidget::on_btnMenu_Max_clicked()
  255. {
  256. static bool max = false;
  257. static QRect location = this->geometry();
  258. if (max)
  259. {
  260. this->setGeometry(location);
  261. btnMenu_Max->setIcon(QIcon(":/image/huanyuan.png"));
  262. btnMenu_Max->setStyleSheet("background:transparent;");
  263. }
  264. else
  265. {
  266. location = this->geometry();
  267. this->setGeometry(qApp->desktop()->availableGeometry());
  268. btnMenu_Max->setIcon(QIcon(":/image/max.png"));
  269. btnMenu_Max->setStyleSheet("background:transparent;");
  270. }
  271. this->setProperty("canMove", max);
  272. max = !max;
  273. }
  274. void QUIWidget::on_btnMenu_Close_clicked()
  275. {
  276. // 1011-5-16,退出时停止所有Task,防止出现崩溃问题
  277. // 但是此时如果还有各个Tool在执行的话,需要提前先退出每一个Tool,要不还是有可能崩溃在Tool里
  278. g_pTaskManager->stopAllTask();
  279. ::Sleep(200);
  280. emit closing();
  281. //exit(0);
  282. QApplication* app;
  283. app->quit();
  284. }
  285. void QUIWidget::dragEnterEvent(QDragEnterEvent* event)
  286. {
  287. event->accept();
  288. QDialog::dragEnterEvent(event);
  289. }
  290. /// <summary>
  291. /// 界面还原后执行刷新
  292. /// </summary>
  293. /// <param name="event"></param>
  294. void QUIWidget::changeEvent(QEvent* event)
  295. {
  296. switch (event->type())
  297. {
  298. case QEvent::WindowStateChange:
  299. // 给MDI框架发送消息,刷新内部所有页面
  300. QApplication::sendEvent(VPGlobal::getMdiFrame(), new QEvent(QResizeEvent::Resize));
  301. // QApplication::sendEvent(VPGlobal::getTaskMonitorView(), new QEvent(QResizeEvent::Resize));
  302. qDebug() << "QUIWidget::changeEvent";
  303. break;
  304. default:
  305. break;
  306. }
  307. }