changeset 0:cb0cde2c5eb9

Initial commit. Basically a Hello World with a Tray Icon.
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 07 Feb 2014 11:41:15 +0000
parents
children 524cd4908656
files CMakeLists.txt ui/icons.qrc ui/img/tray_22.png ui/main.cpp ui/mainwindow.cpp ui/mainwindow.h
diffstat 6 files changed, 195 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Fri Feb 07 11:41:15 2014 +0000
@@ -0,0 +1,57 @@
+cmake_minimum_required(VERSION 2.8.8)
+project(m13)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
+
+# Use cmake's automoc and make sure the generated files are included
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+find_package(Qt5Widgets REQUIRED)
+
+include_directories(${Qt5Widgets_INCLUDE_DIRS})
+add_definitions(${Qt5Widgets_DEFINITIONS})
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
+
+set(M13UI_SOURCES
+    ui/main.cpp
+    ui/mainwindow.cpp
+)
+
+set(M13UI_RESOURCES
+    ui/icons.qrc
+)
+
+if(UNIX)
+   # See: https://bugreports.qt-project.org/browse/QTBUG-35918
+   # XCB_EXTRA_LIBS should be gotten automatically.
+   # The following list is taken from the .pri file
+   get_target_property(_loc Qt5::Widgets LOCATION)
+   get_filename_component(_qtpath ${_loc} PATH)
+   set(XCB_EXTRA_LIBS
+      -L${_qtpath}
+      -lX11 -lX11-xcb -lXi -lxcb-render-util -lXrender -lSM -lICE -lxcb-render -ldbus-1
+      -lxcb -lxcb-image -lxcb-icccm -lxcb-sync -lxcb-xfixes -lxcb-shm -lxcb-randr
+      -lxcb-shape -lxcb-keysyms -lQt5PlatformSupport
+      -lfreetype -lQt5DBus -lQt5Gui -ljpeg -lpng -lQt5Core
+      -lz -lm -ldl -lrt -lpthread)
+
+   set(EXTRA_STATIC_LIBS -lz -lpthread -ldl -lpng -ljpeg
+      Qt5::QXcbIntegrationPlugin ${XCB_EXTRA_LIBS})
+elseif(WIN32)
+   get_target_property(_loc Qt5::Widgets LOCATION)
+   get_filename_component(_qtpath ${_loc} PATH)
+   set(WINDOWS_EXTRA_LIBS
+        -L${_qtpath}
+        -lwinspool -lshlwapi -lfreetype -lbz2 -lpng16
+        -lQt5PlatformSupport -lQt5Gui -lcomdlg32 -loleaut32 -limm32 -lwinmm
+        -lglu32 -lopengl32 -lgdi32 -ljpeg -lpng -lQt5Core -lole32 -luuid -lws2_32
+        -ladvapi32 -lshell32 -luser32 -lkernel32 -lz -lsicuin -lsicuuc -lsicudt -lpcre16)
+   set(EXTRA_STATIC_LIBS Qt5::QWindowsIntegrationPlugin ${WINDOWS_EXTRA_LIBS})
+endif()
+
+qt5_add_resources(M13UI_SOURCES ${M13UI_RESOURCES})
+
+add_executable(m13ui ${M13UI_SOURCES})
+
+target_link_libraries(m13ui Qt5::Widgets ${EXTRA_STATIC_LIBS})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/icons.qrc	Fri Feb 07 11:41:15 2014 +0000
@@ -0,0 +1,5 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource>
+    <file>img/tray_22.png</file>
+</qresource>
+</RCC>
Binary file ui/img/tray_22.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/main.cpp	Fri Feb 07 11:41:15 2014 +0000
@@ -0,0 +1,32 @@
+#include "mainwindow.h"
+
+#include <QApplication>
+#include <QSystemTrayIcon>
+#include <QtPlugin>
+#include <QMessageBox>
+
+
+#ifdef Q_OS_WIN
+ Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
+#else
+ Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
+#endif
+
+int main(int argc, char **argv)
+{
+    QApplication app (argc, argv);
+
+    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
+        QMessageBox::critical(0, QObject::tr("m13ui"),
+                              QObject::tr("Couldn't detect any system tray "
+                                          "on this system. This software can only "
+                                          "be used in a desktop environment."));
+        return 1;
+    }
+    QApplication::setQuitOnLastWindowClosed(false);
+
+    MainWindow mainWin;
+    mainWin.show();
+
+    return app.exec();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/mainwindow.cpp	Fri Feb 07 11:41:15 2014 +0000
@@ -0,0 +1,66 @@
+#include "mainwindow.h"
+
+#include <QDebug>
+#include <QMessageBox>
+#include <QSystemTrayIcon>
+#include <QAction>
+#include <QDialog>
+#include <QMenu>
+
+MainWindow::MainWindow() {
+    createActions();
+    createTrayIcon();
+
+    connect(mTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
+            this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
+}
+
+void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
+{
+    showMessage();
+    qDebug() << "Activated for reason: " << reason;
+    switch (reason) {
+    case QSystemTrayIcon::Trigger:
+    case QSystemTrayIcon::DoubleClick:
+    case QSystemTrayIcon::MiddleClick:
+        showMessage();
+        break;
+    default:
+        ;
+    }
+}
+
+void MainWindow::showMessage()
+{
+    mTrayIcon->showMessage("Hello", "World", QSystemTrayIcon::Information,
+                          10000);
+}
+void MainWindow::manualCheck()
+{
+    QMessageBox::information(0, "Yay",
+                             "It Woarx.\n");
+}
+
+void MainWindow::createActions()
+{
+    mCheckUpdates = new QAction(tr("Check for Updates"), this);
+    connect(mCheckUpdates, SIGNAL(triggered()), this, SLOT(manualCheck()));
+}
+
+void MainWindow::createTrayIcon()
+{
+    QIcon trayImg = QIcon(":/img/tray_22.png");
+
+    qDebug() << "Creating tray icon";
+    mTrayMenu = new QMenu(this);
+    mTrayMenu->addAction(mCheckUpdates);
+
+    mTrayIcon = new QSystemTrayIcon(this);
+    mTrayIcon->setContextMenu(mTrayMenu);
+
+    mTrayIcon->setIcon(trayImg);
+    setWindowIcon(trayImg);
+    mTrayIcon->show();
+    mTrayIcon->setToolTip(tr("m13ui"));
+    showMessage();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/mainwindow.h	Fri Feb 07 11:41:15 2014 +0000
@@ -0,0 +1,35 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QSystemTrayIcon>
+#include <QDialog>
+class QMenu;
+class QAction;
+
+class MainWindow : public QDialog
+{
+    Q_OBJECT
+
+public:
+    MainWindow();
+
+    void setMessage(const QString message) {mCurMessage = message;}
+    QString getMessage() {return mCurMessage;}
+
+private slots:
+    void showMessage();
+    void iconActivated(QSystemTrayIcon::ActivationReason reason);
+    void manualCheck();
+
+private:
+    void createTrayIcon();
+    void createActions();
+    
+    QString mCurMessage;
+
+    QSystemTrayIcon *mTrayIcon;
+    QMenu *mTrayMenu;
+    QAction *mCheckUpdates;
+};
+
+#endif // MAINWINDOW_H

http://wald.intevation.org/projects/trustbridge/