Commit defe20e4 authored by NICLAUSSE Nicolas's avatar NICLAUSSE Nicolas

add method to buld the bar from the factory; add optional submenus

parent 927ff338
......@@ -411,17 +411,21 @@ dummyWindowBar::dummyWindowBar(QWidget *parent) : QFrame(parent)
dtkWidgetsWorkspaceBar *bar = new dtkWidgetsWorkspaceBar;
bar->setStack(this->stack);
// For a dynamic bar with a botton that pops a menu to create new workspace:
// For a dynamic bar with a botton that pops a menu to create new workspace:
//
// bar->addWorkspaceInMenu("dummyWorkspaceA", "My Dummy Workspace A");
// bar->addWorkspaceInMenu("dummyWorkspaceB", "My Dummy Workspace B", fa::image, "category1");
// bar->addWorkspaceInMenu("dummyWorkspaceC", "dtk", fa::image, "category1");
// bar->addWorkspaceInMenu("My Dummy Workspace A", fa::sitemap, "dummyWorkspaceA" );
// bar->addWorkspaceInMenu("My Dummy Workspace B", fa::image, "dummyWorkspaceB" );
// bar->addWorkspaceInMenu("dtk", fa::image, "dummyWorkspaceC" );
// here we use a static workspace bar
// Another option is to build automatically the bar from the factory keys
bar->buildFromFactory();
bar->setDynamic(false);
bar->createWorkspace("My Dummy Workspace A", "dummyWorkspaceA", false );
bar->createWorkspace("My Dummy Workspace B", "dummyWorkspaceB", false );
// here we use a static workspace bar
//
// bar->setDynamic(false);
// bar->createWorkspace("My Dummy Workspace A", "dummyWorkspaceA", false );
// bar->createWorkspace("My Dummy Workspace B", "dummyWorkspaceB", false );
QVBoxLayout *l = new QVBoxLayout(this);
l->setContentsMargins(0, 0, 0, 0);
......
......@@ -180,7 +180,6 @@ public:
public:
bool inside = false;
int n_workspace = 0;
public:
QHBoxLayout *layout;
......@@ -196,8 +195,9 @@ public:
void createWorkspace(const QColor &, const QString &, bool display_destroy);
public:
QList<QColor> colors;
QStringList workspaces;
QList<QColor> colors;
QList<QMenu *> submenus;
QStringList workspaces;
};
void dtkWidgetsWorkspaceBarPrivate::createWorkspace(const QColor & color, const QString& name, bool display_destroy)
......@@ -235,12 +235,10 @@ void dtkWidgetsWorkspaceBarPrivate::createWorkspace(const QColor & color, const
this->onItemClicked(this->items.count()-1);
}
void dtkWidgetsWorkspaceBarPrivate::onItemClicked(int index)
{
q->emit indexChanged(index);
//TODO highlight index
this->stack->setCurrentIndex(index);
for (dtkWidgetsWorkspaceBarItem *item : items) {
......@@ -248,7 +246,6 @@ void dtkWidgetsWorkspaceBarPrivate::onItemClicked(int index)
QColor color = this->colors.at(this->workspaces.indexOf(item->text(),0));
item->setStyleSheet(QString("font-size: %1px; font-style: %2; color: rgb(%3,%4,%5);").arg(current_index == index ? "24" : "12").arg(current_index == index ? "bold" : "normal").arg(color.red()).arg(color.green()).arg(color.blue()));
}
}
// ///////////////////////////////////////////////////////////////////
......@@ -266,7 +263,6 @@ dtkWidgetsWorkspaceBar::dtkWidgetsWorkspaceBar(QWidget *parent): QFrame(parent),
d->layout = new QHBoxLayout(this);
d->layout->addWidget(d->button);
// connect(item, &dtkWidgetsWorkspaceBarItem::clicked, [=] () { d->onItemClicked(0); });
connect(d, SIGNAL(indexDeleted(int)), this, SIGNAL(indexDeleted(int)));
QShortcut *nextWS = new QShortcut(QKeySequence(Qt::ControlModifier + Qt::ShiftModifier + Qt::Key_PageDown), this);
......@@ -284,8 +280,6 @@ dtkWidgetsWorkspaceBar::dtkWidgetsWorkspaceBar(QWidget *parent): QFrame(parent),
this->setCurrentIndex(index);
});
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
this->setMouseTracking(true);
}
......@@ -301,32 +295,72 @@ void dtkWidgetsWorkspaceBar::setDynamic(bool value)
}
void dtkWidgetsWorkspaceBar::addWorkspaceInMenu(const QString& workspace_name, int icon, const QString & type)
void dtkWidgetsWorkspaceBar::addWorkspaceInMenu(const QString & type, const QString& workspace_name, int icon, const QString & group)
{
d->workspaces << workspace_name;
QAction *action = d->button->menu->addAction(dtkFontAwesome::instance()->icon(icon), workspace_name);
if (!dtk::widgets::workspace::pluginFactory().keys().contains(type)) {
qWarning() << "type" << type << "unknown in factory";
return;
}
QString name = workspace_name;
if (name.isEmpty())
name = type;
if (icon == 0)
icon = fa::pluscircle;
d->workspaces << name;
QMenu *menu = nullptr;
if (!group.isEmpty()) {
for (QMenu *smenu: d->submenus) {
if (smenu->title() == group) {
menu = smenu;
break;
}
}
if (!menu) {
menu = d->button->menu->addMenu(dtkFontAwesome::instance()->icon(fa::image), group);
menu->setTitle(group);
d->submenus << menu;
}
}
if (!menu)
menu = d->button->menu;
QAction *action = menu->addAction(dtkFontAwesome::instance()->icon(icon), name);
connect(action, &QAction::triggered, [=] (bool triggered) {
this->createWorkspace(workspace_name, type);
this->createWorkspace(name, type);
});
}
void dtkWidgetsWorkspaceBar::createWorkspace(const QString &name, const QString& type, bool display_destroy)
{
QColor color = d->colors.at(d->workspaces.indexOf(name,0) % d->colors.count()) ;
d->createWorkspace(color, name, display_destroy);
dtkWidgetsWorkspace *workspace = dtk::widgets::workspace::pluginFactory().create(type);
if (!workspace) {
qWarning() << "can't create workspace of type" << type << dtk::widgets::workspace::pluginFactory().keys();
return;
}
QColor color = d->colors.at(d->workspaces.indexOf(name,0) % d->colors.count()) ;
d->createWorkspace(color, name, display_destroy);
d->stack->addWidget(workspace);
d->stack->setCurrentWidget(workspace);
}
dtkWidgetsWorkspaceBar::~dtkWidgetsWorkspaceBar(void)
{
d->colors.clear();
d->submenus.clear();
d->workspaces.clear();
delete d;
}
......@@ -355,4 +389,11 @@ void dtkWidgetsWorkspaceBar::setCurrentIndex(int i)
d->onItemClicked(i);
}
void dtkWidgetsWorkspaceBar::buildFromFactory(void)
{
for (const QString &key: dtk::widgets::workspace::pluginFactory().keys()) {
this->addWorkspaceInMenu(key);
}
}
#include "dtkWidgetsWorkspaceBar.moc"
......@@ -14,7 +14,7 @@ public:
dtkWidgetsWorkspaceBar(QWidget *parent = nullptr);
virtual ~dtkWidgetsWorkspaceBar();
public:
public:
void setStack(QStackedWidget *stack);
signals:
......@@ -22,7 +22,10 @@ signals:
void indexDeleted(int);
public slots:
void addWorkspaceInMenu(const QString& workspace_name, int icon, const QString& type);
void addWorkspaceInMenu(const QString& type, const QString& workspace_name = QString(), int icon = 0, const QString& group = QString());
public:
void buildFromFactory(void);
public slots:
void createWorkspace(const QString &name, const QString& type, bool display_destroy = true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment