mainwindow-common.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. mephi-tasks — a client to NRNU MEPhI Redmine server
  3. Copyright (C) 2015 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <QSettings>
  16. #include <QMenu>
  17. #include "mainwindow-common.h"
  18. MainWindowCommon::MainWindowCommon ( QWidget *parent ) :
  19. QMainWindow ( parent )
  20. {
  21. QSettings qsettings ( settings.settingsFilePath, QSettings::IniFormat );
  22. this->sortFunctMap.insert ( SORT_UNDEFINED, NULL );
  23. this->sortFunctMap.insert ( SORT_NAME_ASC, issueCmpFunct_name_lt );
  24. this->sortFunctMap.insert ( SORT_NAME_DESC, issueCmpFunct_name_gt );
  25. this->sortFunctMap.insert ( SORT_ASSIGNEE_ASC, issueCmpFunct_assignee_lt );
  26. this->sortFunctMap.insert ( SORT_ASSIGNEE_DESC, issueCmpFunct_assignee_gt );
  27. this->sortFunctMap.insert ( SORT_DUE_TO_ASC, issueCmpFunct_dueTo_lt );
  28. this->sortFunctMap.insert ( SORT_DUE_TO_DESC, issueCmpFunct_dueTo_gt );
  29. this->sortFunctMap.insert ( SORT_STATUS_POS_ASC, issueCmpFunct_statusPosition_lt );
  30. this->sortFunctMap.insert ( SORT_STATUS_POS_DESC, issueCmpFunct_statusPosition_gt );
  31. this->sortFunctMap.insert ( SORT_UPDATED_ON_ASC, issueCmpFunct_updatedOn_lt );
  32. this->sortFunctMap.insert ( SORT_UPDATED_ON_DESC, issueCmpFunct_updatedOn_gt );
  33. this->sortFunctMap.insert ( SORT_STATUS_ISCLOSED_ASC, issueCmpFunct_statusIsClosed_lt );
  34. this->sortFunctMap.insert ( SORT_TIMEENTRY_FROM_ASC, timeEntryCmpFunct_from_lt );
  35. memset ( this->sortColumn, 0, sizeof ( this->sortColumn ) );
  36. this->sortColumn[0] = ( MainWindowCommon::ESortColumn ) qsettings.value ( "sortcode" ).toInt();
  37. this->sortLogicalIndex = qsettings.value ( "sortcolumn" ).toInt();
  38. this->sortOrder = ( Qt::SortOrder ) qsettings.value ( "sortorder" ).toInt();
  39. this->createIconComboBox();
  40. this->createTrayIcon();
  41. this->status ( GOOD );
  42. this->setIcon ( GOOD );
  43. this->trayIcon->show();
  44. this->logTimeWindow = NULL;
  45. return;
  46. }
  47. MainWindowCommon::~MainWindowCommon()
  48. {
  49. QSettings qsettings ( settings.settingsFilePath, QSettings::IniFormat );
  50. qsettings.setValue ( "sortcode", ( int ) this->sortColumn[0] );
  51. qsettings.setValue ( "sortcolumn", ( int ) this->sortLogicalIndex );
  52. qsettings.setValue ( "sortorder", ( int ) this->sortOrder );
  53. return;
  54. }
  55. /**** updateRoles ****/
  56. void MainWindowCommon::get_roles_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  57. {
  58. ( void ) reply;
  59. ( void ) arg;
  60. QJsonArray roles = json->object() ["roles"].toArray();
  61. this->roles.set ( roles );
  62. return;
  63. }
  64. int MainWindowCommon::updateRoles()
  65. {
  66. redmine->get_roles ( ( Redmine::callback_t ) &MainWindowCommon::get_roles_callback, this );
  67. return 0;
  68. }
  69. /**** updateMemberships ****/
  70. void MainWindowCommon::get_memberships_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  71. {
  72. ( void ) reply;
  73. ( void ) arg;
  74. QJsonArray memberships = json->object() ["memberships"].toArray();
  75. this->memberships.set ( memberships );
  76. return;
  77. }
  78. int MainWindowCommon::updateMemberships()
  79. {
  80. redmine->get_memberships ( ( Redmine::callback_t ) &MainWindowCommon::get_memberships_callback, this );
  81. return 0;
  82. }
  83. /**** /updateMemberships ****/
  84. /**** updateEnumerations ****/
  85. void MainWindowCommon::get_enumerations_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  86. {
  87. ( void ) reply;
  88. ( void ) arg;
  89. QJsonArray enumerations = json->object() ["enumerations"].toArray();
  90. this->enumerations.set ( enumerations );
  91. return;
  92. }
  93. int MainWindowCommon::updateEnumerations()
  94. {
  95. redmine->get_enumerations ( ( Redmine::callback_t ) &MainWindowCommon::get_enumerations_callback, this );
  96. return 0;
  97. }
  98. /**** /updateEnumerations ****/
  99. /**** updateProjects ****/
  100. void MainWindowCommon::projects_display()
  101. {
  102. qFatal ( "projects_display() is not re-implemented by the derivative object" );
  103. }
  104. void MainWindowCommon::get_projects_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  105. {
  106. ( void ) reply;
  107. ( void ) arg;
  108. this->updateProjectsMutex.lock(); // is not a thread-safe function, locking
  109. QJsonObject answer = json->object();
  110. QJsonArray projects = answer["projects"].toArray();
  111. this->projects.set ( projects );
  112. this->projects_display();
  113. this->updateProjectsMutex.unlock();
  114. return;
  115. }
  116. QList<QJsonObject> MainWindowCommon::issues_get_byProjectId ( int project_id )
  117. {
  118. return this->issues_byProjectId[project_id];
  119. }
  120. int MainWindowCommon::updateProjects()
  121. {
  122. redmine->get_projects ( ( Redmine::callback_t ) &MainWindowCommon::get_projects_callback, this );
  123. return 0;
  124. }
  125. /**** /updateProjects ****/
  126. /**** updateIssues ****/
  127. void MainWindowCommon::issues_display()
  128. {
  129. qFatal ( "issues_display() is not re-implemented by the derivative object" );
  130. }
  131. void MainWindowCommon::get_issues_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  132. {
  133. ( void ) reply;
  134. ( void ) arg;
  135. this->updateIssuesMutex.lock(); // is not a thread-safe function, locking
  136. QJsonObject answer = json->object();
  137. QJsonArray issues = answer["issues"].toArray();
  138. this->issues.set ( issues );
  139. this->issues_display();
  140. this->updateIssuesMutex.unlock();
  141. return;
  142. }
  143. int MainWindowCommon::updateIssues()
  144. {
  145. redmine->get_issues ( ( Redmine::callback_t ) &MainWindowCommon::get_issues_callback, this, false, "status=*&limit=10000" );
  146. return 0;
  147. }
  148. /**** /updateIssues ****/
  149. /**** tray-related stuff ****/
  150. void MainWindowCommon::createTrayIcon()
  151. {
  152. this->trayIconMenu = new QMenu ( this );
  153. this->trayIcon = new QSystemTrayIcon ( this );
  154. this->trayIcon->setContextMenu ( trayIconMenu );
  155. return;
  156. }
  157. void MainWindowCommon::setIcon ( EIcon index )
  158. {
  159. //qDebug("icon: %i", index);
  160. QIcon icon = this->iconComboBox.itemIcon ( index );
  161. this->trayIcon->setIcon ( icon );
  162. this->setWindowIcon ( icon );
  163. this->trayIcon->setToolTip ( this->iconComboBox.itemText ( index ) );
  164. }
  165. void MainWindowCommon::createIconComboBox()
  166. {
  167. this->iconComboBox.addItem ( QIcon ( ":/images/good.png" ), tr ( "Просроченных задач нет" ) );
  168. this->iconComboBox.addItem ( QIcon ( ":/images/bad.png" ), tr ( "Есть просроченные задачи" ) );
  169. return;
  170. }
  171. void MainWindowCommon::on_closeLogTimeWindow()
  172. {
  173. this->logTimeWindow = NULL;
  174. return;
  175. }
  176. void MainWindowCommon::openLogTimeWindow()
  177. {
  178. if (this->logTimeWindow != NULL)
  179. delete this->logTimeWindow;
  180. this->logTimeWindow = new LogTimeWindow;
  181. connect ( this->logTimeWindow, SIGNAL ( on_destructor() ), this, SLOT ( on_closeLogTimeWindow() ) );
  182. this->logTimeWindow->show();
  183. return;
  184. }
  185. void MainWindowCommon::on_closeShowTimeWindow()
  186. {
  187. this->showTimeWindow = NULL;
  188. return;
  189. }
  190. void MainWindowCommon::openShowTimeWindow()
  191. {
  192. if (this->showTimeWindow != NULL)
  193. delete this->showTimeWindow;
  194. this->showTimeWindow = new ShowTimeWindow;
  195. connect ( this->showTimeWindow, SIGNAL ( on_destructor() ), this, SLOT ( on_closeShowTimeWindow() ) );
  196. this->showTimeWindow->show();
  197. return;
  198. }
  199. void MainWindowCommon::showOnTop()
  200. {
  201. #ifdef Q_OS_WIN32
  202. // raise() doesn't work :(
  203. Qt::WindowFlags flags_old = this->windowFlags();
  204. Qt::WindowFlags flags_ontop = flags_old | Qt::WindowStaysOnTopHint;
  205. this->setWindowFlags ( flags_ontop );
  206. this->show();
  207. this->setWindowFlags ( flags_old );
  208. this->show();
  209. #else
  210. this->show();
  211. this->raise();
  212. #endif
  213. return;
  214. }
  215. void MainWindowCommon::toggleShowHide()
  216. {
  217. if ( this->isVisible() ) {
  218. settings.hideOnStart = true;
  219. this->hide();
  220. } else {
  221. settings.hideOnStart = false;
  222. this->showOnTop();
  223. }
  224. return;
  225. }
  226. /**** /tray-related stuff ****/