Refactored the handling for widgets in mainwindow to use the new class PluginTree
This commit is contained in:
parent
38b430626f
commit
97892546de
5 changed files with 103 additions and 26 deletions
|
@ -8,13 +8,15 @@ SOURCES += main.cpp \
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
tellduscenterapplication.cpp \
|
tellduscenterapplication.cpp \
|
||||||
autoupdater.cpp \
|
autoupdater.cpp \
|
||||||
message.cpp
|
message.cpp \
|
||||||
|
plugintree.cpp
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
tellduscenterapplication.h \
|
tellduscenterapplication.h \
|
||||||
autoupdater.h \
|
autoupdater.h \
|
||||||
CocoaInitializer.h \
|
CocoaInitializer.h \
|
||||||
message.h \
|
message.h \
|
||||||
tellduscenterplugin.h
|
tellduscenterplugin.h \
|
||||||
|
plugintree.h
|
||||||
RESOURCES += resource.qrc
|
RESOURCES += resource.qrc
|
||||||
TARGET = TelldusCenter
|
TARGET = TelldusCenter
|
||||||
macx {
|
macx {
|
||||||
|
|
|
@ -14,18 +14,22 @@
|
||||||
#include "tellduscenterapplication.h"
|
#include "tellduscenterapplication.h"
|
||||||
#include "tellduscenterplugin.h"
|
#include "tellduscenterplugin.h"
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
|
#include "plugintree.h"
|
||||||
|
|
||||||
class MainWindowPrivate {
|
class MainWindowPrivate {
|
||||||
public:
|
public:
|
||||||
QToolBar *pagesBar;
|
QToolBar *pagesBar;
|
||||||
Message *message;
|
Message *message;
|
||||||
QStackedLayout *stackedLayout;
|
QStackedLayout *stackedLayout;
|
||||||
|
PluginTree pluginTree;
|
||||||
|
QActionGroup *pagesActionGroup;
|
||||||
};
|
};
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
|
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
|
||||||
: QMainWindow(parent, flags)
|
: QMainWindow(parent, flags)
|
||||||
{
|
{
|
||||||
d = new MainWindowPrivate;
|
d = new MainWindowPrivate;
|
||||||
|
d->pagesActionGroup = new QActionGroup( this );
|
||||||
d->message = new Message(this);
|
d->message = new Message(this);
|
||||||
connect(qApp, SIGNAL(showMessage(QString,QString,QString)), d->message, SLOT(showMessage(QString,QString,QString)));
|
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 = addToolBar(tr("Pages"));
|
||||||
d->pagesBar->setIconSize(QSize(32, 32));
|
d->pagesBar->setIconSize(QSize(32, 32));
|
||||||
|
|
||||||
QActionGroup *ag = new QActionGroup(this);
|
|
||||||
|
|
||||||
TelldusCenterApplication *app = TelldusCenterApplication::instance();
|
TelldusCenterApplication *app = TelldusCenterApplication::instance();
|
||||||
PluginList plugins = app->plugins();
|
PluginList plugins = app->plugins();
|
||||||
if (!plugins.empty()) {
|
if (!plugins.empty()) {
|
||||||
|
@ -95,30 +97,32 @@ void MainWindow::setupToolBar()
|
||||||
foreach( TelldusCenterPlugin *plugin, plugins ) {
|
foreach( TelldusCenterPlugin *plugin, plugins ) {
|
||||||
QStringList widgets = plugin->widgets();
|
QStringList widgets = plugin->widgets();
|
||||||
foreach( QString widget, widgets ) {
|
foreach( QString widget, widgets ) {
|
||||||
QString page = widget.section('.', 0, 0);
|
this->addWidget( widget, plugin->iconForPage( d->pluginTree.page(widget) ), plugin->widget(widget, this) );
|
||||||
if (!toolbarIcons.contains( page )) {
|
|
||||||
QWidget *pageWidget = plugin->widget( page, this );
|
|
||||||
if (!pageWidget) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
QAction *action = new QAction( plugin->iconForPage( page ), page, this );
|
}
|
||||||
|
}
|
||||||
|
if (!d->pagesActionGroup->actions().empty()) {
|
||||||
|
d->pagesActionGroup->actions().first()->setChecked( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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->setCheckable( true );
|
||||||
action->setChecked( false );
|
action->setChecked( false );
|
||||||
|
|
||||||
int index = d->stackedLayout->addWidget( pageWidget );
|
int index = d->stackedLayout->addWidget( widget );
|
||||||
action->setData( index );
|
action->setData( index );
|
||||||
|
|
||||||
connect(action, SIGNAL(triggered()), this, SLOT(slotPagesClick()));
|
connect(action, SIGNAL(triggered()), this, SLOT(slotPagesClick()));
|
||||||
ag->addAction( action );
|
d->pagesActionGroup->addAction( action );
|
||||||
toolbarIcons.insert(page);
|
//toolbarIcons.insert(page);
|
||||||
}
|
d->pagesBar->addAction( action );
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if (!ag->actions().empty()) {
|
|
||||||
ag->actions().first()->setChecked( true );
|
|
||||||
}
|
|
||||||
d->pagesBar->addActions( ag->actions() );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,9 @@ public:
|
||||||
MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
|
MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void addWidget( const QString &page, const QIcon &icon, QWidget *widget );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void closeEvent( QCloseEvent *event );
|
virtual void closeEvent( QCloseEvent *event );
|
||||||
|
|
||||||
|
|
43
telldus-gui/TelldusCenter/plugintree.cpp
Normal file
43
telldus-gui/TelldusCenter/plugintree.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include "plugintree.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
typedef QHash<QString, QHash<QString, QWidget *> > 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;
|
||||||
|
}
|
25
telldus-gui/TelldusCenter/plugintree.h
Normal file
25
telldus-gui/TelldusCenter/plugintree.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef PLUGINTREE_H
|
||||||
|
#define PLUGINTREE_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
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
|
Loading…
Add table
Add a link
Reference in a new issue