window_demo_qt.cpp

Go to the documentation of this file.
00001 #include "window_demo_qt.hpp"
00002 #include "guilog.h"
00003 
00004 #include "application_config.h"
00005 #include "rk_remokon.h"
00006 #include "up_uploader.h"
00007 
00008 #include "common/QsKineticScroller.hpp"
00009 
00010 #include <QAbstractListModel>
00011 #include <QAction>
00012 #include <QApplication>
00013 #include <QErrorMessage>
00014 #include <QListView>
00015 #include <QMenu>
00016 #include <QMenuBar>
00017 #include <QObject>
00018 #include <QString>
00019 #include <QStringListModel>
00020 
00021 #include <QtGlobal>
00022 
00023 #include <stdio.h>
00024 
00025 #define MY_MAX_ROWS 100
00026 
00027 static QStringListModel* gModel = NULL;
00028 
00029 #if __FEATURE_GUILOG__
00030 
00031 void guilog(const QString& s)
00032 {
00033   int count = gModel->rowCount();
00034   if (count >= MY_MAX_ROWS) {
00035     int newCount = count * 3 / 4;
00036     gModel->removeRows(newCount, count - newCount);
00037   }
00038 
00039   int ix = 0;
00040   gModel->insertRow(ix);
00041   gModel->setData(gModel->index(ix), s, Qt::DisplayRole);
00042 }
00043 
00044 void guilog(const char* s)
00045 {
00046   guilog(QString::fromUtf8(s));
00047 }
00048 
00049 extern "C" void guilogf(const char* fmt, ...)
00050 {
00051   va_list argp;
00052   va_start(argp, fmt);
00053   char buf[256];
00054   vsnprintf(buf, 256, fmt, argp);
00055   guilog(buf);
00056   va_end(argp);
00057 }
00058 
00059 #endif
00060 
00061 MainWindow::MainWindow(QWidget *parent)
00062     : QMainWindow(parent)
00063 {
00064   listView = new QListView(this);
00065   // Required by QsKineticScroller.
00066   listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
00067   iScroller = new QsKineticScroller(this);
00068   iScroller->enableKineticScrollFor(listView);
00069   // The view does not take ownership of the model.
00070   gModel = new QStringListModel(stringList, listView);
00071   listView->setModel(gModel);
00072   listView->setViewMode(QListView::ListMode);
00073   listView->setSelectionMode(QAbstractItemView::SingleSelection);
00074   listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
00075 
00076   setCentralWidget(listView);
00077 
00078   QAction *quitAction = new QAction(tr("&Quit"), this);
00079   quitAction->setShortcuts(QKeySequence::Quit); // C-q on Linux
00080   quitAction->setStatusTip(tr("Stop logger and quit"));
00081   connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
00082   addAction(quitAction);
00083 
00084   QAction* uploadAction = new QAction(tr("&Upload now"), this);
00085   uploadAction->setStatusTip(tr("Take log snapshot and upload now"));
00086   connect(uploadAction, SIGNAL(triggered()), this, SLOT(uploadNow()));
00087 
00088   QAction* remokonAction = new QAction(tr("&Enable remote control"), this);
00089   remokonAction->setStatusTip(tr("Allow remote control for a while"));
00090   connect(remokonAction, SIGNAL(triggered()), this, SLOT(remokonNow()));
00091 
00092 #if defined(__SYMBIAN32__)
00093   QMenuBar* actionMenu = menuBar();
00094 #else
00095   QMenu* actionMenu = menuBar()->addMenu(tr("&Action"));
00096 #endif /* __SYMBIAN32__ */
00097   actionMenu->addAction(uploadAction);
00098   actionMenu->addAction(remokonAction);
00099   actionMenu->addSeparator();
00100   actionMenu->addAction(quitAction);
00101 }
00102 
00103 static void showFreeGerror(GError* error)
00104 {
00105   if (error) {
00106     gchar* s = gx_error_to_string(error);
00107     gx_error_free(error);
00108     QErrorMessage::qtHandler()->showMessage(QString(s));
00109     g_free(s);
00110   } else {
00111     QErrorMessage::qtHandler()->showMessage(QString("out of memory"));
00112   }
00113 }
00114 
00115 void MainWindow::uploadNow()
00116 {
00117 #if __FEATURE_UPLOADER__
00118   up_Uploader* uploader = ac_global_Uploader;
00119   GError* localError = NULL;
00120   if (!up_Uploader_upload_now(uploader, &localError)) {
00121     showFreeGerror(localError);
00122   }
00123 #endif
00124 }
00125 
00126 void MainWindow::remokonNow()
00127 {
00128 #if __FEATURE_REMOKON__
00129   rk_Remokon* remokon = ac_global_Remokon;
00130   GError* localError = NULL;
00131   if (!rk_Remokon_start_timed(remokon, 5 * 60, &localError)) {
00132     showFreeGerror(localError);
00133   }
00134 #endif
00135 }

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1