mainwindow.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (C) 2010 Martin Zuber, zuber@centrum.cz
  3. This application is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This application is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with This application; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include "mainwindow.h"
  16. #include "ui_mainwindow.h"
  17. #include <QDebug>
  18. #include <QResizeEvent>
  19. #include <QLayoutItem>
  20. #include <QAction>
  21. #include <QMenuBar>
  22. #include "feggclockwidget.h"
  23. #include "dsettings.h"
  24. /**
  25. Initializes the main form, connects signals.
  26. @param parent parent widget, should be null
  27. */
  28. MainWindow::MainWindow(QWidget *parent) :
  29. QMainWindow(parent),
  30. ui(new Ui::MainWindow)
  31. {
  32. ui->setupUi(this);
  33. grid = new QGridLayout(this);
  34. m_clocks = new FEggClockWidget* [3];
  35. for(int i = 0; i < 3; i++) {
  36. m_clocks[i] = new FEggClockWidget();
  37. }
  38. m_option = new QAction(tr("Options"), this);
  39. m_option->setSoftKeyRole(QAction::PositiveSoftKey);
  40. connect(m_option, SIGNAL(triggered()), this, SLOT(onOptionTriggered()));
  41. addAction(m_option);
  42. // workaround: the softkeys are not visible in simulator
  43. #if defined(Q_WS_SIMULATOR)
  44. QMenuBar *menubar = new QMenuBar(this);
  45. menubar->addAction(m_option);
  46. this->setMenuBar(menubar);
  47. #endif
  48. }
  49. /**
  50. Delete all dynamic data
  51. */
  52. MainWindow::~MainWindow()
  53. {
  54. // delete widgets
  55. for(int i = 0; i < 3; i++) delete m_clocks[i];
  56. // delete array
  57. delete m_clocks;
  58. // delete ui
  59. delete ui;
  60. }
  61. /**
  62. Resize handling, there are two modes:
  63. <ul>
  64. <li>portrait mode - there are 3 widgets in one column</li>
  65. <li>landscape mode - 2 widgets in one row</li>
  66. </ul>
  67. When the main form is in portrait mode, the 3rd widget
  68. is stopped, reset and hidden.
  69. @param e resize event data
  70. */
  71. void MainWindow::resizeEvent(QResizeEvent *e)
  72. {
  73. // get the size parts
  74. int w = this->size().width();
  75. int h = this->size().height();
  76. // delete old grid
  77. delete grid;
  78. // create new one
  79. // TODO: is there way to "reconfigure" the widgets in a grid?
  80. grid = new QGridLayout();
  81. for(int i = 0; i < 2; i++) grid->removeWidget(m_clocks[i]);
  82. if(w > h)
  83. {
  84. // landscape mode - there is only place for 2 clock widgets
  85. // TODO: Verify on real device the 2x2 matrix
  86. grid->addWidget(m_clocks[0], 0, 0);
  87. grid->addWidget(m_clocks[1], 1, 0);
  88. // invisible item is stopped and reset
  89. m_clocks[2]->stopAlertAndReset();
  90. // ... and hidden
  91. m_clocks[2]->setVisible(false);
  92. }
  93. else
  94. {
  95. // portrait mode - 3 widgets in one column are enough
  96. grid->addWidget(m_clocks[0], 0, 0);
  97. grid->addWidget(m_clocks[1], 1, 0);
  98. grid->addWidget(m_clocks[2], 2, 0);
  99. // the last one should be visible
  100. m_clocks[2]->setVisible(true);
  101. }
  102. // place grid and layout
  103. ui->centralWidget->setLayout(grid);
  104. grid->layout();
  105. }
  106. void MainWindow::onOptionTriggered()
  107. {
  108. qDebug() << "Option action triggered";
  109. DSettings dlg(this);
  110. dlg.exec();
  111. }