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 \
|
||||
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 {
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
|
||||
|
|
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