showtimewindow.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "showtimewindow.h"
  2. #include "ui_showtimewindow.h"
  3. ShowTimeWindow::ShowTimeWindow(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::ShowTimeWindow)
  6. {
  7. ui->setupUi(this);
  8. connect ( redmine, SIGNAL ( callback_call ( void*, callback_t, QNetworkReply*, QJsonDocument*, void* ) ),
  9. this, SLOT ( callback_dispatcher ( void*, callback_t, QNetworkReply*, QJsonDocument*, void* ) ) );
  10. this->updateUsers();
  11. this->updateTimeEntries(0);
  12. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 0, QHeaderView::Interactive );
  13. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 1, QHeaderView::Interactive );
  14. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 2, QHeaderView::Interactive );
  15. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 3, QHeaderView::Interactive );
  16. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 4, QHeaderView::Interactive );
  17. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 5, QHeaderView::Interactive );
  18. #ifdef __MOBILE__
  19. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 6, QHeaderView::Interactive );
  20. this->ui->timeEntries->horizontalHeader()->resizeSection ( 0, 80 );
  21. this->ui->timeEntries->horizontalHeader()->resizeSection ( 1, 80 );
  22. this->ui->timeEntries->horizontalHeader()->resizeSection ( 2, 72 );
  23. this->ui->timeEntries->horizontalHeader()->resizeSection ( 3, 104 );
  24. this->ui->timeEntries->horizontalHeader()->resizeSection ( 4, 82 );
  25. this->ui->timeEntries->horizontalHeader()->resizeSection ( 5, 500 );
  26. this->ui->timeEntries->horizontalHeader()->resizeSection ( 6, 500 );
  27. #else
  28. this->ui->timeEntries->horizontalHeader()->setSectionResizeMode ( 6, QHeaderView::Stretch );
  29. this->ui->timeEntries->horizontalHeader()->resizeSection ( 0, 50 );
  30. this->ui->timeEntries->horizontalHeader()->resizeSection ( 1, 50 );
  31. this->ui->timeEntries->horizontalHeader()->resizeSection ( 2, 45 );
  32. this->ui->timeEntries->horizontalHeader()->resizeSection ( 3, 70 );
  33. this->ui->timeEntries->horizontalHeader()->resizeSection ( 4, 60 );
  34. this->ui->timeEntries->horizontalHeader()->resizeSection ( 5, 200 );
  35. this->ui->date->setMaximumHeight(120);
  36. #endif
  37. this->ui->timeEntries->horizontalHeader()->setSortIndicatorShown ( true );
  38. this->ui->date->setSelectedDate(QDate::currentDate());
  39. }
  40. ShowTimeWindow::~ShowTimeWindow()
  41. {
  42. delete ui;
  43. }
  44. /********* updateUsers *********/
  45. void ShowTimeWindow::updateUsers_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  46. {
  47. ( void ) reply;
  48. ( void ) arg;
  49. QJsonArray users = json->object() ["users"].toArray();
  50. qDebug ( "ShowTimeWindow::updateUsers_callback: users.count() == %i", users.count() );
  51. this->ui->user->clear();
  52. this->ui->user->addItem ( "Я", 0 );
  53. foreach (const QJsonValue &userV, users) {
  54. QJsonObject user = userV.toObject();
  55. int userId = user["id"].toInt();
  56. QString userDisplayName = user["name"].toString();
  57. this->ui->user->addItem ( userDisplayName, userId );
  58. // To make a cache:
  59. redmine->get_time_entries( userId, NULL, NULL, NULL, false, "limit=1000" );
  60. }
  61. return;
  62. }
  63. void ShowTimeWindow::updateUsers()
  64. {
  65. redmine->get_stuff_to_do ( this, ( Redmine::callback_t ) &ShowTimeWindow::updateUsers_callback );
  66. return;
  67. }
  68. /********* /updateUsers *********/
  69. /********* updateTimeEntries *********/
  70. void ShowTimeWindow::timeEntries_display()
  71. {
  72. QList<QJsonObject> list;
  73. //this->ui->timeEntries->clear();
  74. //this->ui->timeEntries->setRowCount ( this->timeEntries.count() );
  75. QDate dateSelected = this->ui->date->selectedDate();
  76. foreach (const QJsonValue &timeEntryV, this->timeEntries) {
  77. QJsonObject timeEntry = timeEntryV.toObject();
  78. QDateTime from = QDateTime::fromString(timeEntry ["from"].toString(), Qt::ISODate);
  79. QDate date = from.date();
  80. if (date != dateSelected)
  81. continue;
  82. list.append(timeEntry);
  83. }
  84. qSort ( list.begin(), list.end(), timeEntryCmpFunct_from_lt );
  85. this->ui->timeEntries->setRowCount ( list.size() );
  86. for (int row = 0; row < list.size(); ++row) {
  87. QJsonObject timeEntry = list.at(row);
  88. QDateTime from = QDateTime::fromString(timeEntry ["from"].toString(), Qt::ISODate);
  89. QDateTime to = QDateTime::fromString(timeEntry ["to" ].toString(), Qt::ISODate);
  90. int sec_diff = from.time().secsTo(to.time());
  91. //qDebug ( "\"from\": \"%s\", sec_diff == %i; from.minute() == %i; to.minute() == %i", timeEntry ["from"].toString().toStdString().c_str(), sec_diff, from.time().minute(), to.time().minute() );
  92. this->ui->timeEntries->setItem(row, 0, new QTableWidgetItem ( from.toString("hh:mm") ) );
  93. this->ui->timeEntries->setItem(row, 1, new QTableWidgetItem ( to .toString("hh:mm") ) );
  94. this->ui->timeEntries->setItem(row, 2, new QTableWidgetItem ( QString::number( sec_diff/60 ) ) );
  95. this->ui->timeEntries->setItem(row, 3, new QTableWidgetItem ( timeEntry ["activity"].toObject() ["name"].toString() ) );
  96. if (timeEntry ["issue"].toObject().empty()) {
  97. QString projectIdentifier = timeEntry ["project"].toObject() ["identifier"].toString();
  98. QString projectName;
  99. QString userLogin = timeEntry ["user"].toObject() ["login"].toString();
  100. if ( projectIdentifier.toLower() == userLogin.toLower() ) {
  101. projectName = "Личный проект";
  102. } else {
  103. projectName = timeEntry ["project"].toObject() ["name"].toString();
  104. }
  105. //qDebug(("projectIdentifier == \""+projectIdentifier+"\"; userLogin == \""+userLogin+"\"; projectName == \""+projectName+"\"").toStdString().c_str());
  106. this->ui->timeEntries->setItem(row, 4, new QTableWidgetItem ( projectIdentifier ) );
  107. this->ui->timeEntries->setItem(row, 5, new QTableWidgetItem ( projectName ) );
  108. } else {
  109. this->ui->timeEntries->setItem(row, 4, new QTableWidgetItem ( QString::number(timeEntry ["issue"].toObject() ["id"].toInt()) ) );
  110. this->ui->timeEntries->setItem(row, 5, new QTableWidgetItem ( timeEntry ["issue"].toObject() ["subject"].toString() ) );
  111. }
  112. this->ui->timeEntries->setItem(row, 6, new QTableWidgetItem ( timeEntry ["comments"].toString() ) );
  113. }
  114. }
  115. void ShowTimeWindow::updateTimeEntries_callback ( QNetworkReply *reply, QJsonDocument *json, void *arg )
  116. {
  117. ( void ) reply;
  118. ( void ) arg;
  119. this->timeEntries = json->object() ["time_entries"].toArray();
  120. qDebug ( "ShowTimeWindow::updateTimeEntries_callback: timeEntries.count() == %i", timeEntries.count() );
  121. this->timeEntries_display();
  122. return;
  123. }
  124. void ShowTimeWindow::updateTimeEntries(int userId)
  125. {
  126. redmine->get_time_entries( userId, this, ( Redmine::callback_t ) &ShowTimeWindow::updateTimeEntries_callback, NULL, false, "limit=1000" );
  127. return;
  128. }
  129. /********* /updateTimeEntries *********/
  130. void ShowTimeWindow::on_closeButton_clicked()
  131. {
  132. delete this;
  133. }
  134. void ShowTimeWindow::on_date_selectionChanged()
  135. {
  136. this->timeEntries_display();
  137. }
  138. void ShowTimeWindow::on_timeEntries_itemSelectionChanged()
  139. {
  140. QTableWidget *timeEntries = this->ui->timeEntries;
  141. int columns_count = timeEntries->columnCount();
  142. int rows_count = timeEntries->rowCount();
  143. QList<QTableWidgetSelectionRange> selected_list = timeEntries->selectedRanges();
  144. foreach ( QTableWidgetSelectionRange range, selected_list ) {
  145. if ( range.leftColumn() != 0 || range.rightColumn() != columns_count - 1 )
  146. timeEntries->setRangeSelected (
  147. QTableWidgetSelectionRange (
  148. range.topRow(), 0,
  149. range.bottomRow(), columns_count - 1
  150. ),
  151. true
  152. );
  153. else
  154. /* Workaround: Drop selection if everything is selected
  155. * it's required to do not select everything on sort switching
  156. */
  157. if ( range.leftColumn() == 0 && range.rightColumn() == columns_count - 1 &&
  158. range.topRow() == 0 && range.bottomRow() == rows_count - 1 ) {
  159. timeEntries->setRangeSelected (
  160. QTableWidgetSelectionRange ( 0, 0, rows_count - 1, columns_count - 1 ),
  161. false
  162. );
  163. break;
  164. }
  165. }
  166. }
  167. void ShowTimeWindow::on_user_currentIndexChanged(int index)
  168. {
  169. (void) index;
  170. int userId = this->ui->user->currentData().toInt();
  171. this->updateTimeEntries(userId);
  172. return;
  173. }