Use dialog instead of popup menu.
authorMax Lapan <max.lapan@gmail.com>
Thu, 18 Mar 2010 10:56:47 +0000 (13:56 +0300)
committerMax Lapan <max.lapan@gmail.com>
Thu, 18 Mar 2010 10:56:47 +0000 (13:56 +0300)
mainwidget.cpp
mainwidget.hpp
menudialog.cpp [new file with mode: 0644]
menudialog.hpp [new file with mode: 0644]
yandex-traffic-gui.pri
yandex-traffic-widget_ru.qm
yandex-traffic-widget_ru.ts

index 7e9a455..33d3303 100644 (file)
@@ -6,6 +6,7 @@
 #include "devstate.hpp"
 #include "settings.hpp"
 #include "log.hpp"
+#include "menudialog.hpp"
 
 
 // --------------------------------------------------
@@ -177,24 +178,25 @@ void MainWidget::applySettings ()
 }
 
 
-void MainWidget::mousePressEvent (QMouseEvent *event)
+bool MainWidget::event (QEvent *event)
 {
-    QMenu menu;
-    QAction *settingsAction, *updateAction, *todo;
+    if (event->type () != QEvent::WindowActivate)
+        return QWidget::event (event);
 
-    Log::instance ()->add (QString ("mousePressEvent at %1,%2").arg (event->pos ().x ()).arg (event->pos ().y ()));
+    MenuDialog menu (tr ("Yandex.Traffic"));
 
-    settingsAction = menu.addAction (tr ("Settings"));
-    updateAction = menu.addAction (tr ("Update"));
+    menu.addEntry (tr ("Settings")).addEntry (tr ("Update"));
 
-    todo = menu.exec (event->pos ());
-    if (!todo)
-        return;
+    switch (menu.run ()) {
+        case 0:
+            settingsDialog ();
+            break;
+        case 1:
+            _traffic->update ();
+            break;
+    }
 
-    if (todo == settingsAction)
-        settingsDialog ();
-    if (todo == updateAction)
-        _traffic->update ();
+    return QWidget::event (event);
 }
 
 
index eee235f..20e86be 100644 (file)
@@ -33,8 +33,8 @@ public slots:
     void updateData ();
 
 protected:
+    bool event (QEvent *event);
     void paintEvent (QPaintEvent *event);
-    void mousePressEvent (QMouseEvent *event);
 
     void updateSize ();
     void applySettings ();
diff --git a/menudialog.cpp b/menudialog.cpp
new file mode 100644 (file)
index 0000000..a7d68c9
--- /dev/null
@@ -0,0 +1,52 @@
+#include <QtGui>
+
+#include "menudialog.hpp"
+
+
+// --------------------------------------------------
+// MenuDialog
+// --------------------------------------------------
+MenuDialog::MenuDialog (const QString &title)
+    : QDialog ()
+{
+    setWindowTitle (title);
+    _layout = new QVBoxLayout (this);
+}
+
+
+MenuDialog& MenuDialog::addEntry (const QString &text)
+{
+    QPushButton* btn = new QPushButton (text, this);
+
+    connect (btn, SIGNAL (clicked ()), SLOT (buttonClicked ()));
+    _layout->addWidget (btn);
+    _buttons.append (btn);
+
+    return *this;
+}
+
+
+void MenuDialog::buttonClicked ()
+{
+    QPushButton *btn = static_cast<QPushButton*> (sender ());
+
+    if (!btn)
+        return;
+
+    index = _buttons.indexOf (btn);
+
+    accept ();
+}
+
+
+int MenuDialog::run ()
+{
+    index = -1;
+
+    if (exec () == QDialog::Accepted)
+        return index;
+    else
+        return -1;
+}
+
+
diff --git a/menudialog.hpp b/menudialog.hpp
new file mode 100644 (file)
index 0000000..be9878d
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef __MENUDIALOG_H__
+#define __MENUDIALOG_H__
+
+#include <QtGui>
+
+
+class MenuDialog : public QDialog
+{
+    Q_OBJECT
+private:
+    QList<QPushButton*> _buttons;
+    QBoxLayout* _layout;
+    int index;
+
+protected slots:
+    void buttonClicked ();
+
+public:
+    MenuDialog (const QString &title);
+    MenuDialog& addEntry (const QString &text);
+    int run ();
+};
+
+#endif // __MENUDIALOG_H__
index 72bf043..2d919c2 100644 (file)
@@ -1,5 +1,5 @@
-HEADERS += $$PWD/mainwidget.hpp $$PWD/light.hpp $$PWD/settingsDialog.hpp
-SOURCES += $$PWD/mainwidget.cpp $$PWD/light.cpp $$PWD/settingsDialog.cpp
+HEADERS += $$PWD/mainwidget.hpp $$PWD/light.hpp $$PWD/settingsDialog.hpp $$PWD/menudialog.hpp
+SOURCES += $$PWD/mainwidget.cpp $$PWD/light.cpp $$PWD/settingsDialog.cpp $$PWD/menudialog.cpp
 
 !maemo5 {
         HEADERS += $$PWD/QtMaemo5Adapters.hpp
index 0e10b41..e3e7c86 100644 (file)
Binary files a/yandex-traffic-widget_ru.qm and b/yandex-traffic-widget_ru.qm differ
index 2faa161..62bdc8e 100644 (file)
             <numerusform>%n баллов</numerusform>
         </translation>
     </message>
+    <message>
+        <source>Yandex.Traffic</source>
+        <translation>Яндекс.Пробки</translation>
+    </message>
 </context>
 <context>
     <name>Settings</name>