New version: 1.0.1
[ameter] / mainwindow.cpp
1 #include <QtCore/QCoreApplication>
2
3 #include "mainwindow.h"
4 #include "asettings.h"
5 #include "about.h"
6
7 extern qreal g_n;
8 extern qreal a_max;
9 extern int divisions;
10 extern int angle_step;
11 extern int smoothing;
12
13 int data_rate = 0;
14
15 MainWindow::MainWindow(QWidget *parent)
16         : QMainWindow(parent)
17 {
18         QMenuBar *bar = menuBar();
19         QAction *action;
20         QSettings settings("igorinov", "ameter", this);
21
22         g_n = settings.value("g", g_n).toDouble();
23         a_max = settings.value("max", a_max).toDouble();
24         divisions = settings.value("divisions", divisions).toInt();
25         angle_step = settings.value("angle_step", angle_step).toInt();
26         smoothing = settings.value("smoothing", smoothing).toInt();
27         data_rate = settings.value("rate", data_rate).toInt();
28
29         awidget = new AMeterWidget(this);
30         setCentralWidget(awidget);
31         action = bar->addAction("&About");
32         connect(action, SIGNAL(triggered()), this, SLOT(showAbout()));
33         action = bar->addAction("&Settings");
34         connect(action, SIGNAL(triggered()), this, SLOT(showSettings()));
35         accelerometer = new QAccelerometer(this);
36         accelerometer->setProperty("alwaysOn", true);
37         accelerometer->addFilter(awidget);
38         accelerometer->setDataRate(data_rate);
39         accelerometer->start();
40         
41 }
42
43 MainWindow::~MainWindow()
44 {
45         delete awidget;
46         delete accelerometer;
47 }
48
49 void MainWindow::showAbout()
50 {
51         AboutDialog dialog(this);
52
53         dialog.exec();
54 }
55
56 void MainWindow::showSettings()
57 {
58         SettingsDialog dialog(this);
59         QSettings settings("igorinov", "ameter", this);
60
61         if (dialog.exec() != QDialog::Accepted)
62         {
63                 return;
64         }
65         
66         settings.setValue("g", g_n);
67         settings.setValue("max", a_max);
68         settings.setValue("divisions", divisions);
69         settings.setValue("angle_step", angle_step);
70         settings.setValue("rate", data_rate);
71
72         awidget->setGravity(g_n);
73         accelerometer->stop();
74         accelerometer->setDataRate(data_rate);
75         accelerometer->start();
76 }
77
78 void MainWindow::setOrientation(ScreenOrientation orientation)
79 {
80 #if defined(Q_OS_SYMBIAN)
81     // If the version of Qt on the device is < 4.7.2, that attribute won't work
82     if (orientation != ScreenOrientationAuto) {
83         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
84         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
85             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
86             return;
87         }
88     }
89 #endif // Q_OS_SYMBIAN
90
91     Qt::WidgetAttribute attribute;
92     switch (orientation) {
93 #if QT_VERSION < 0x040702
94     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
95     case ScreenOrientationLockPortrait:
96         attribute = static_cast<Qt::WidgetAttribute>(128);
97         break;
98     case ScreenOrientationLockLandscape:
99         attribute = static_cast<Qt::WidgetAttribute>(129);
100         break;
101     default:
102     case ScreenOrientationAuto:
103         attribute = static_cast<Qt::WidgetAttribute>(130);
104         break;
105 #else // QT_VERSION < 0x040702
106     case ScreenOrientationLockPortrait:
107         attribute = Qt::WA_LockPortraitOrientation;
108         break;
109     case ScreenOrientationLockLandscape:
110         attribute = Qt::WA_LockLandscapeOrientation;
111         break;
112     default:
113     case ScreenOrientationAuto:
114         attribute = Qt::WA_AutoOrientation;
115         break;
116 #endif // QT_VERSION < 0x040702
117     };
118     setAttribute(attribute, true);
119 }
120
121 void MainWindow::showExpanded()
122 {
123 #ifdef Q_OS_SYMBIAN
124     showFullScreen();
125 #elif defined(Q_WS_MAEMO_5)
126     showMaximized();
127 #else
128     show();
129 #endif
130 }
131