Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

daterangeselector.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*694:*/
  2. #line 70 "./daterangeselector.w"
  3. #include <QCalendarWidget>
  4. #include <QPushButton>
  5. #include <QBoxLayout>
  6. #include <QLabel>
  7. #include <QToolButton>
  8. #include <QApplication>
  9. #include <QDesktopWidget>
  10. #include "daterangeselector.h"
  11. /*696:*/
  12. #line 117 "./daterangeselector.w"
  13. CustomDateRangePopup::CustomDateRangePopup(QWidget*parent):
  14. QWidget(parent,Qt::Popup),startDateSelector(new QCalendarWidget),
  15. endDateSelector(new QCalendarWidget),applyButton(new QPushButton(tr("Apply")))
  16. {
  17. setAttribute(Qt::WA_WindowPropagation);
  18. QVBoxLayout*outerLayout= new QVBoxLayout;
  19. QHBoxLayout*calendarsLayout= new QHBoxLayout;
  20. QVBoxLayout*startDateLayout= new QVBoxLayout;
  21. QVBoxLayout*endDateLayout= new QVBoxLayout;
  22. QHBoxLayout*buttonLayout= new QHBoxLayout;
  23. QLabel*startDateLabel= new QLabel(tr("From"));
  24. QLabel*endDateLabel= new QLabel(tr("To"));
  25. startDateSelector->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
  26. endDateSelector->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
  27. DateRangeSelector*selector= qobject_cast<DateRangeSelector*> (parent);
  28. if(parent){
  29. QStringList range= selector->currentRange().toStringList();
  30. startDateSelector->setSelectedDate(QDate::fromString(range.first(),Qt::ISODate));
  31. endDateSelector->setSelectedDate(QDate::fromString(range.last(),Qt::ISODate));
  32. }
  33. connect(startDateSelector,SIGNAL(selectionChanged()),this,SLOT(validateRange()));
  34. connect(endDateSelector,SIGNAL(selectionChanged()),this,SLOT(validateRange()));
  35. startDateLayout->addWidget(startDateLabel);
  36. startDateLayout->addWidget(startDateSelector);
  37. endDateLayout->addWidget(endDateLabel);
  38. endDateLayout->addWidget(endDateSelector);
  39. connect(applyButton,SIGNAL(clicked()),this,SLOT(applyRange()));
  40. buttonLayout->addStretch();
  41. buttonLayout->addWidget(applyButton);
  42. calendarsLayout->addLayout(startDateLayout);
  43. calendarsLayout->addLayout(endDateLayout);
  44. outerLayout->addLayout(calendarsLayout);
  45. outerLayout->addLayout(buttonLayout);
  46. setLayout(outerLayout);
  47. }
  48. /*:696*//*697:*/
  49. #line 165 "./daterangeselector.w"
  50. void CustomDateRangePopup::hideEvent(QHideEvent*)
  51. {
  52. emit hidingPopup();
  53. }
  54. /*:697*//*698:*/
  55. #line 174 "./daterangeselector.w"
  56. void CustomDateRangePopup::applyRange()
  57. {
  58. DateRangeSelector*selector= qobject_cast<DateRangeSelector*> (parentWidget());
  59. if(selector)
  60. {
  61. selector->setCustomRange(QVariant(QStringList()<<
  62. startDateSelector->selectedDate().toString(Qt::ISODate)<<
  63. endDateSelector->selectedDate().toString(Qt::ISODate)));
  64. }
  65. hide();
  66. }
  67. /*:698*//*699:*/
  68. #line 191 "./daterangeselector.w"
  69. void CustomDateRangePopup::validateRange()
  70. {
  71. if(startDateSelector->selectedDate()> endDateSelector->selectedDate())
  72. {
  73. applyButton->setEnabled(false);
  74. }
  75. else
  76. {
  77. applyButton->setEnabled(true);
  78. }
  79. }
  80. /*:699*/
  81. #line 81 "./daterangeselector.w"
  82. /*700:*/
  83. #line 209 "./daterangeselector.w"
  84. DateRangeSelector::DateRangeSelector(QWidget*parent):
  85. QWidget(parent),quickSelector(new QComboBox(this)),
  86. customRangeSelector(NULL),lastIndex(0)
  87. {
  88. connect(quickSelector,SIGNAL(currentIndexChanged(int)),this,SLOT(updateRange(int)));
  89. QDate currentDate= QDate::currentDate();
  90. QHBoxLayout*layout= new QHBoxLayout;
  91. /*701:*/
  92. #line 238 "./daterangeselector.w"
  93. quickSelector->addItem("Yesterday",QVariant(QStringList()<<
  94. currentDate.addDays(-1).toString(Qt::ISODate)));
  95. quickSelector->addItem("Today",QVariant(QStringList()<<
  96. currentDate.toString(Qt::ISODate)));
  97. quickSelector->insertSeparator(quickSelector->count());
  98. quickSelector->addItem("This Week",QVariant(QStringList()<<
  99. (currentDate.dayOfWeek()%7?
  100. currentDate.addDays(-currentDate.dayOfWeek()).toString(Qt::ISODate):
  101. currentDate.toString(Qt::ISODate))<<
  102. currentDate.addDays(6-(currentDate.dayOfWeek()%7)).toString(Qt::ISODate)));
  103. quickSelector->addItem("This Week to Date",currentDate.dayOfWeek()%7?
  104. QVariant(QStringList()<<
  105. currentDate.addDays(-currentDate.dayOfWeek()).toString(Qt::ISODate)<<
  106. currentDate.toString(Qt::ISODate)):
  107. QVariant(QStringList()<<currentDate.toString(Qt::ISODate)));
  108. quickSelector->addItem("Last Week",QVariant(QStringList()<<
  109. currentDate.addDays(-(currentDate.dayOfWeek()%7)-7).toString(Qt::ISODate)<<
  110. currentDate.addDays(-(currentDate.dayOfWeek()%7)-1).toString(Qt::ISODate)));
  111. quickSelector->addItem("Last 7 Days",QVariant(QStringList()<<
  112. currentDate.addDays(-6).toString(Qt::ISODate)<<
  113. currentDate.toString(Qt::ISODate)));
  114. quickSelector->insertSeparator(quickSelector->count());
  115. quickSelector->addItem("This Month",QVariant(QStringList()<<
  116. QDate(currentDate.year(),currentDate.month(),1).toString(Qt::ISODate)<<
  117. QDate(currentDate.year(),currentDate.month(),
  118. currentDate.daysInMonth()).toString(Qt::ISODate)));
  119. quickSelector->addItem("This Month to Date",(currentDate.day()==1?
  120. (QVariant(QStringList()<<currentDate.toString(Qt::ISODate))):
  121. (QVariant(QStringList()<<
  122. QDate(currentDate.year(),currentDate.month(),1).toString(Qt::ISODate)<<
  123. currentDate.toString(Qt::ISODate)))));
  124. quickSelector->addItem("Last Four Weeks",QVariant(QStringList()<<
  125. currentDate.addDays(-27).toString(Qt::ISODate)<<
  126. currentDate.toString(Qt::ISODate)));
  127. quickSelector->addItem("Last 30 Days",QVariant(QStringList()<<
  128. currentDate.addDays(-29).toString(Qt::ISODate)<<
  129. currentDate.toString(Qt::ISODate)));
  130. quickSelector->insertSeparator(quickSelector->count());
  131. quickSelector->addItem("This Quarter",QVariant(QStringList()<<
  132. QDate(currentDate.year(),currentDate.month()-((currentDate.month()-1)%3),1).toString(Qt::ISODate)<<
  133. (currentDate.month()> 9?
  134. QDate(currentDate.year(),12,31).toString(Qt::ISODate):
  135. QDate(currentDate.year(),currentDate.month()-((currentDate.month()-1)%3)+3,1).addDays(-1).toString(Qt::ISODate))));
  136. quickSelector->addItem("This Quarter to Date",
  137. (currentDate.day()==1&&(currentDate.month()-1)%3==0)?
  138. QVariant(QStringList()<<currentDate.toString(Qt::ISODate)):
  139. QVariant(QStringList()<<
  140. QDate(currentDate.year(),currentDate.month()-((currentDate.month()-1)%3),1).toString(Qt::ISODate)<<
  141. currentDate.toString(Qt::ISODate)));
  142. quickSelector->addItem("Last Quarter",currentDate.month()<4?
  143. QVariant(QStringList()<<
  144. QDate(currentDate.year()-1,10,1).toString(Qt::ISODate)<<
  145. QDate(currentDate.year()-1,12,31).toString(Qt::ISODate)):
  146. QVariant(QStringList()<<
  147. QDate(currentDate.year(),currentDate.month()-((currentDate.month()-1)%3)-3,1).toString(Qt::ISODate)<<
  148. QDate(currentDate.year(),currentDate.month()-((currentDate.month()-1)%3),1).addDays(-1).toString(Qt::ISODate)));
  149. quickSelector->addItem("Last 90 Days",QVariant(QStringList()<<
  150. currentDate.addDays(-89).toString(Qt::ISODate)<<
  151. currentDate.toString(Qt::ISODate)));
  152. quickSelector->insertSeparator(quickSelector->count());
  153. quickSelector->addItem("This Year",QVariant(QStringList()<<
  154. QDate(currentDate.year(),1,1).toString(Qt::ISODate)<<
  155. QDate(currentDate.year(),12,31).toString(Qt::ISODate)));
  156. quickSelector->addItem("This Year to Date",(currentDate.dayOfYear()==1)?
  157. QVariant(QStringList()<<currentDate.toString(Qt::ISODate)):
  158. QVariant(QStringList()<<QDate(currentDate.year(),1,1).toString(Qt::ISODate)<<
  159. currentDate.toString(Qt::ISODate)));
  160. quickSelector->addItem("Last Year",QVariant(QStringList()<<
  161. QDate(currentDate.year()-1,1,1).toString(Qt::ISODate)<<
  162. QDate(currentDate.year()-1,12,31).toString(Qt::ISODate)));
  163. quickSelector->addItem("Last 365 Days",QVariant(QStringList()<<
  164. currentDate.addDays(-364).toString(Qt::ISODate)<<
  165. currentDate.toString(Qt::ISODate)));
  166. quickSelector->insertSeparator(quickSelector->count());
  167. quickSelector->addItem("Lifetime");
  168. quickSelector->addItem("Custom");
  169. /*:701*/
  170. #line 219 "./daterangeselector.w"
  171. QToolButton*customButton= new QToolButton;
  172. customButton->setIcon(QIcon::fromTheme("office-calendar",
  173. QIcon(":/resources/icons/tango/scalable/apps/office-calendar.svg")));
  174. layout->addWidget(quickSelector);
  175. layout->addWidget(customButton);
  176. setLayout(layout);
  177. connect(customButton,SIGNAL(clicked()),this,SLOT(toggleCustom()));
  178. }
  179. /*:700*//*702:*/
  180. #line 321 "./daterangeselector.w"
  181. void DateRangeSelector::updateRange(int index)
  182. {
  183. if(index!=lastIndex&&index==quickSelector->count()-1)
  184. {
  185. toggleCustom();
  186. }
  187. else
  188. {
  189. lastIndex= index;
  190. emit rangeUpdated(quickSelector->itemData(quickSelector->currentIndex()));
  191. }
  192. }
  193. /*:702*//*703:*/
  194. #line 338 "./daterangeselector.w"
  195. void DateRangeSelector::popupHidden()
  196. {
  197. customRangeSelector->deleteLater();
  198. customRangeSelector= NULL;
  199. quickSelector->setCurrentIndex(lastIndex);
  200. }
  201. /*:703*//*704:*/
  202. #line 349 "./daterangeselector.w"
  203. void DateRangeSelector::setCustomRange(QVariant range)
  204. {
  205. quickSelector->setItemData(quickSelector->count()-1,range);
  206. emit rangeUpdated(range);
  207. lastIndex= quickSelector->count()-1;
  208. quickSelector->setCurrentIndex(lastIndex);
  209. }
  210. /*:704*//*705:*/
  211. #line 364 "./daterangeselector.w"
  212. void DateRangeSelector::toggleCustom()
  213. {
  214. if(!customRangeSelector){
  215. customRangeSelector= new CustomDateRangePopup(this);
  216. QPoint pos= rect().bottomLeft();
  217. QPoint pos2= rect().topLeft();
  218. pos= mapToGlobal(pos);
  219. pos2= mapToGlobal(pos2);
  220. QSize size= customRangeSelector->sizeHint();
  221. QRect screen= QApplication::desktop()->availableGeometry(pos);
  222. if(pos.x()+size.width()> screen.right()){
  223. pos.setX(screen.right()-size.width());
  224. }
  225. pos.setX(qMax(pos.x(),screen.left()));
  226. if(pos.y()+size.height()> screen.bottom()){
  227. pos.setY(pos2.y()-size.height());
  228. }else if(pos.y()<screen.top()){
  229. pos.setY(screen.top());
  230. }
  231. if(pos.y()<screen.top()){
  232. pos.setY(screen.top());
  233. }
  234. if(pos.y()+size.height()> screen.bottom()){
  235. pos.setY(screen.bottom()-size.height());
  236. }
  237. customRangeSelector->move(pos);
  238. customRangeSelector->show();
  239. connect(customRangeSelector,SIGNAL(hidingPopup()),
  240. this,SLOT(popupHidden()));
  241. }
  242. else
  243. {
  244. customRangeSelector->close();
  245. customRangeSelector->deleteLater();
  246. customRangeSelector= NULL;
  247. }
  248. }
  249. /*:705*//*706:*/
  250. #line 406 "./daterangeselector.w"
  251. QVariant DateRangeSelector::currentRange()
  252. {
  253. return quickSelector->itemData(lastIndex);
  254. }
  255. /*:706*//*707:*/
  256. #line 414 "./daterangeselector.w"
  257. void DateRangeSelector::setCurrentIndex(int index)
  258. {
  259. quickSelector->setCurrentIndex(index);
  260. }
  261. int DateRangeSelector::currentIndex()
  262. {
  263. return quickSelector->currentIndex();
  264. }
  265. /*:707*//*708:*/
  266. #line 434 "./daterangeselector.w"
  267. void DateRangeSelector::setLifetimeRange(QString startDate,QString endDate)
  268. {
  269. quickSelector->setItemData(quickSelector->count()-2,
  270. QVariant(QStringList()<<startDate<<endDate));
  271. }
  272. /*:708*//*709:*/
  273. #line 444 "./daterangeselector.w"
  274. void DateRangeSelector::removeIndex(int index)
  275. {
  276. quickSelector->removeItem(index);
  277. }
  278. /*:709*/
  279. #line 82 "./daterangeselector.w"
  280. #ifdef __unix__
  281. #ifndef __linux__
  282. #include "moc_daterangeselector.cpp"
  283. #endif
  284. #endif
  285. /*:694*/