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