From 97892546de605cfb231439eb8150437d33c4c47f Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Wed, 8 Apr 2009 15:29:45 +0000 Subject: [PATCH] Refactored the handling for widgets in mainwindow to use the new class PluginTree --- telldus-gui/TelldusCenter/TelldusCenter.pro | 6 ++- telldus-gui/TelldusCenter/mainwindow.cpp | 52 +++++++++++---------- telldus-gui/TelldusCenter/mainwindow.h | 3 ++ telldus-gui/TelldusCenter/plugintree.cpp | 43 +++++++++++++++++ telldus-gui/TelldusCenter/plugintree.h | 25 ++++++++++ 5 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 telldus-gui/TelldusCenter/plugintree.cpp create mode 100644 telldus-gui/TelldusCenter/plugintree.h diff --git a/telldus-gui/TelldusCenter/TelldusCenter.pro b/telldus-gui/TelldusCenter/TelldusCenter.pro index 6e004ffd..331c321b 100644 --- a/telldus-gui/TelldusCenter/TelldusCenter.pro +++ b/telldus-gui/TelldusCenter/TelldusCenter.pro @@ -8,13 +8,15 @@ SOURCES += main.cpp \ mainwindow.cpp \ tellduscenterapplication.cpp \ autoupdater.cpp \ - message.cpp + message.cpp \ + plugintree.cpp HEADERS += mainwindow.h \ tellduscenterapplication.h \ autoupdater.h \ CocoaInitializer.h \ message.h \ - tellduscenterplugin.h + tellduscenterplugin.h \ + plugintree.h RESOURCES += resource.qrc TARGET = TelldusCenter macx { diff --git a/telldus-gui/TelldusCenter/mainwindow.cpp b/telldus-gui/TelldusCenter/mainwindow.cpp index 211611e6..63a85711 100644 --- a/telldus-gui/TelldusCenter/mainwindow.cpp +++ b/telldus-gui/TelldusCenter/mainwindow.cpp @@ -14,18 +14,22 @@ #include "tellduscenterapplication.h" #include "tellduscenterplugin.h" #include "message.h" +#include "plugintree.h" class MainWindowPrivate { public: QToolBar *pagesBar; Message *message; QStackedLayout *stackedLayout; + PluginTree pluginTree; + QActionGroup *pagesActionGroup; }; MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { d = new MainWindowPrivate; + d->pagesActionGroup = new QActionGroup( this ); d->message = new Message(this); connect(qApp, SIGNAL(showMessage(QString,QString,QString)), d->message, SLOT(showMessage(QString,QString,QString))); @@ -86,8 +90,6 @@ void MainWindow::setupToolBar() d->pagesBar = addToolBar(tr("Pages")); d->pagesBar->setIconSize(QSize(32, 32)); - QActionGroup *ag = new QActionGroup(this); - TelldusCenterApplication *app = TelldusCenterApplication::instance(); PluginList plugins = app->plugins(); if (!plugins.empty()) { @@ -95,30 +97,32 @@ void MainWindow::setupToolBar() foreach( TelldusCenterPlugin *plugin, plugins ) { QStringList widgets = plugin->widgets(); foreach( QString widget, widgets ) { - QString page = widget.section('.', 0, 0); - if (!toolbarIcons.contains( page )) { - QWidget *pageWidget = plugin->widget( page, this ); - if (!pageWidget) { - continue; - } - QAction *action = new QAction( plugin->iconForPage( page ), page, this ); - action->setCheckable( true ); - action->setChecked( false ); - - int index = d->stackedLayout->addWidget( pageWidget ); - action->setData( index ); - - connect(action, SIGNAL(triggered()), this, SLOT(slotPagesClick())); - ag->addAction( action ); - toolbarIcons.insert(page); - } + this->addWidget( widget, plugin->iconForPage( d->pluginTree.page(widget) ), plugin->widget(widget, this) ); } + } + } + if (!d->pagesActionGroup->actions().empty()) { + d->pagesActionGroup->actions().first()->setChecked( true ); + } +} - } - if (!ag->actions().empty()) { - ag->actions().first()->setChecked( true ); - } - d->pagesBar->addActions( ag->actions() ); +void MainWindow::addWidget( const QString &page, const QIcon &icon, QWidget *widget ) { + QString topLevel = d->pluginTree.page( page ); + + bool pageExists = d->pluginTree.pages().contains( topLevel ); + d->pluginTree.add( page, widget ); + if (!pageExists) { + QAction *action = new QAction( icon, page, this ); + action->setCheckable( true ); + action->setChecked( false ); + + int index = d->stackedLayout->addWidget( widget ); + action->setData( index ); + + connect(action, SIGNAL(triggered()), this, SLOT(slotPagesClick())); + d->pagesActionGroup->addAction( action ); + //toolbarIcons.insert(page); + d->pagesBar->addAction( action ); } } diff --git a/telldus-gui/TelldusCenter/mainwindow.h b/telldus-gui/TelldusCenter/mainwindow.h index f40c035d..8a08f3cb 100644 --- a/telldus-gui/TelldusCenter/mainwindow.h +++ b/telldus-gui/TelldusCenter/mainwindow.h @@ -17,6 +17,9 @@ public: MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0); ~MainWindow(); +public slots: + void addWidget( const QString &page, const QIcon &icon, QWidget *widget ); + protected: virtual void closeEvent( QCloseEvent *event ); diff --git a/telldus-gui/TelldusCenter/plugintree.cpp b/telldus-gui/TelldusCenter/plugintree.cpp new file mode 100644 index 00000000..cac42659 --- /dev/null +++ b/telldus-gui/TelldusCenter/plugintree.cpp @@ -0,0 +1,43 @@ +#include "plugintree.h" + +#include + +typedef QHash > PluginList; + +class PluginTreePrivate { +public: + PluginList plugins; +}; + +PluginTree::PluginTree() + :d( new PluginTreePrivate() ) +{ +} + +PluginTree::~PluginTree() { + delete d; +} + +void PluginTree::add( const QString &path, QWidget *widget ) { + QString topLevel( page(path) ); + QString subLevel( widgetName(path) ); + if (!d->plugins[topLevel].contains( subLevel )) { + d->plugins[topLevel][subLevel] = widget; + } +} + +QString PluginTree::page( const QString &path ) { + return path.section('.', 0, 0); +} + +QString PluginTree::widgetName( const QString &path ) { + return path.section('.', 1, 1); +} + +QStringList PluginTree::pages() const { + QStringList pages; + for( PluginList::const_iterator it = d->plugins.begin(); it != d->plugins.end(); ++it ) { + pages.append( it.key() ); + } + return pages; +} diff --git a/telldus-gui/TelldusCenter/plugintree.h b/telldus-gui/TelldusCenter/plugintree.h new file mode 100644 index 00000000..0abf1a90 --- /dev/null +++ b/telldus-gui/TelldusCenter/plugintree.h @@ -0,0 +1,25 @@ +#ifndef PLUGINTREE_H +#define PLUGINTREE_H + +#include +#include + +class PluginTreePrivate; + +class PluginTree +{ +public: + PluginTree(); + ~PluginTree(); + + void add( const QString &path, QWidget *widget ); + QStringList pages() const; + + static QString page( const QString &path ); + static QString widgetName( const QString &path ); + +private: + PluginTreePrivate *d; +}; + +#endif // PLUGINTREE_H