From 414c109d650a1014e7b84dfb66a00df420d67c97 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 20 Jan 2016 16:43:34 +0100 Subject: Remodel section handling: Use constants --- src/command_line_options.cpp | 18 +++++++++++++----- src/dialog.cpp | 19 +++++++++++++++++-- src/dialog.h | 8 +++++--- src/globals.cpp | 3 +++ src/globals.h | 8 ++++++++ src/main.cpp | 31 +++++++++++++++++++++++++++++++ src/session.h | 10 +++++++++- src/sessiontreeitem.cpp | 42 ++++++++++++++++++++++++++---------------- src/sessiontreeitem.h | 13 +++++++++---- src/sessiontreemodel.cpp | 17 ++++++++++++++--- src/sessiontreemodel.h | 4 +++- src/vsession.cpp | 9 ++++++++- src/vsession.h | 21 ++++++++++++++++----- src/xsession.h | 4 ++-- 14 files changed, 164 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/command_line_options.cpp b/src/command_line_options.cpp index fd72641..bd085fc 100644 --- a/src/command_line_options.cpp +++ b/src/command_line_options.cpp @@ -18,18 +18,20 @@ CommandLineOptions::CommandLineOptions(int argc, char * const argv[]) { {"pvs", no_argument, NULL, 'p'}, {"runscript", no_argument, NULL, 'S'}, {"size", required_argument, NULL, 's'}, + {"tab", required_argument, NULL, 'T'}, {"theme", required_argument, NULL, 't'}, {"url", required_argument, NULL, 'u'}, {"version", no_argument, NULL, 'v'}, {"xpath", required_argument, NULL, 'x'}, - {"tab", required_argument, NULL, 'T'}, + {"location-mode", required_argument, NULL, 'locm'}, + {"template-mode", required_argument, NULL, 'tmpm'}, {0, 0, 0, 0} }; int c; // Again, please sort alphabetically in getopt_long call and switch statement - while ((c = getopt_long(argc, argv, "b:c:Dd:Ff:hP:pSs:t:T:u:vx:?", longOptions, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "b:c:Dd:Ff:hl:P:pSs:t:T:u:vx:?", longOptions, NULL)) != -1) { switch (c) { case 'b': options.insert("base", optarg); @@ -53,15 +55,15 @@ CommandLineOptions::CommandLineOptions(int argc, char * const argv[]) { case '?': options.insert("usage", "usage"); break; - case 'P': - options.insert("pool", optarg); - break; case 'l': options.insert("locations", optarg); break; case 'p': options.insert("pvs", "pvs"); break; + case 'P': + options.insert("pool", optarg); + break; case 'S': options.insert("runscript", optarg); break; @@ -83,6 +85,12 @@ CommandLineOptions::CommandLineOptions(int argc, char * const argv[]) { case 'x': options.insert("xpath", optarg); break; + case 'locm': + options.insert("location-mode", optarg); + break; + case 'tmpm': + options.insert("template-mode", optarg); + break; default: options.insert("error", "error"); break; diff --git a/src/dialog.cpp b/src/dialog.cpp index 1d497d2..e430be5 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -29,6 +29,7 @@ Dialog::Dialog(int defaultTab, QWidget *parent) defaultTab_ = defaultTab; qDebug() << "Default tab: " << defaultTab; userInteracted_ = false; + genericExpandedOnce_ = false; ui->setupUi(this); @@ -150,6 +151,14 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) } } +void Dialog::on_treeView_expanded(const QModelIndex& index) { + SessionTreeItem* item = + static_cast(index.internalPointer()); + if (item->session() == NULL && item->sectionType() == SECTION_GENERIC) { + genericExpandedOnce_ = true; + } +} + void Dialog::addItems(const QList& entries, int tab) { if (tab < 0 || tab > 2) { return; @@ -429,7 +438,7 @@ void Dialog::onTabButtonChanged(int tab) { // give focus to treeView if (!ui->filterEdit->hasFocus()) { - ui->treeView->setFocus(); + ui->filterEdit->setFocus(); } // Update pressed status of buttons @@ -474,7 +483,7 @@ void Dialog::on_filterEdit_textChanged() { } } -void Dialog::setListModel(QAbstractItemModel *model) { +void Dialog::setListModel(SessionTreeModel *model) { QAbstractItemModel *old = NULL; if (ui->treeView->model() == model_[0] || ui->treeView->model() == model_[1] || ui->treeView->model() == model_[2]) { } else { @@ -482,6 +491,12 @@ void Dialog::setListModel(QAbstractItemModel *model) { } ui->treeView->setModel(model); ui->treeView->expandAll(); + if (!genericExpandedOnce_ && g_forLocationHandling == LOCATION_EXCLUSIVE && model->rowCount() > 1) { + QModelIndex index = model->getSection(SECTION_GENERIC); + if (index.isValid()) { + ui->treeView->collapse(index); + } + } if (old != NULL) { old->deleteLater(); } diff --git a/src/dialog.h b/src/dialog.h index 17d5cd4..49c3bb0 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -45,24 +45,26 @@ class Dialog : public QDialog { private: // Private vars n methods Ui::Dialog *ui; - SessionTreeModel *model_[3]; // TODO: Constants/Enum for indices - QPushButton *tabs_[3]; + SessionTreeModel *model_[TAB_COUNT]; + QPushButton *tabs_[TAB_COUNT]; QPoint oldCenter_; QTimer *centerTimer_; QString autoStartEntry_; int activeTab_; int defaultTab_; bool userInteracted_; + bool genericExpandedOnce_; QString strings_[STR__MAX]; void onTabButtonChanged(int tab); void configClearButton(); - void setListModel(QAbstractItemModel *model); + void setListModel(SessionTreeModel *model); void keyPressEvent(QKeyEvent * e); private slots: void on_pushButtonStart_clicked(); void on_pushButtonAbort_clicked(); void on_treeView_doubleClicked(const QModelIndex& index); + void on_treeView_expanded(const QModelIndex& index); void on_tabButtonLocal_clicked(); void on_tabButtonMyClasses_clicked(); void on_tabButtonAllClasses_clicked(); diff --git a/src/globals.cpp b/src/globals.cpp index 37c36f0..3262089 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -34,6 +34,9 @@ QString urlBase; bool _fullscreen = false; +int g_templateHandling = TEMPLATES_IGNORE; +int g_forLocationHandling = LOCATION_BUMP; + bool myLessThan(Session* a, Session* b) { return *a < *b; } diff --git a/src/globals.h b/src/globals.h index b943238..60f3d31 100644 --- a/src/globals.h +++ b/src/globals.h @@ -54,6 +54,14 @@ extern QString urlBase; extern bool _fullscreen; +#define TEMPLATES_IGNORE (0) +#define TEMPLATES_BUMP (1) +extern int g_templateHandling; +#define LOCATION_IGNORE (0) +#define LOCATION_BUMP (1) +#define LOCATION_EXCLUSIVE (2) +extern int g_forLocationHandling; + bool myLessThan(Session* a, Session* b); #endif diff --git a/src/main.cpp b/src/main.cpp index 4d0053e..0a4f6cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -35,12 +36,14 @@ int main(int argc, char *argv[]) { " -d, --default name of default session\n" " -c, --config alternative config file\n" " -l, --location location id(s), space separated\n" + " --location-mode how to treat entries for this location (IGNORE, BUMP or EXCLUSIVE)\n" " -P, --pool one or more pool names to display (comma separated)\n" " -f, --file direct boot .desktop file\n" " -x, --xpath path of X Session .desktop files\n" " -u, --url url of vmware .xml file\n" " -s, --size window size x\n" " -t, --theme theme\n" + " --template-mode how to treat template entries (IGNORE or BUMP)\n" " -p, --pvs show pvs options\n" " -D, --debug print debug information\n" " -v, --version print version and exit\n" @@ -200,6 +203,34 @@ int main(int argc, char *argv[]) { basePath = settings.value("path").toString(); } + if (cmdOptions.contains("location-mode")) { + QString mode = cmdOptions.value("location-mode"); + if (mode == "IGNORE") { + g_forLocationHandling = LOCATION_IGNORE; + } else if (mode == "BUMP") { + g_forLocationHandling = LOCATION_BUMP; + } else if (mode == "EXCLUSIVE") { + g_forLocationHandling = LOCATION_EXCLUSIVE; + } else { + qDebug() << "Invalid location mode: " << mode; + QMessageBox::critical(NULL, "Error", "Invalid location mode: " + mode); + return 1; + } + } + + if (cmdOptions.contains("template-mode")) { + QString mode = cmdOptions.value("template-mode"); + if (mode == "IGNORE") { + g_forLocationHandling = LOCATION_IGNORE; + } else if (mode == "BUMP") { + g_forLocationHandling = LOCATION_BUMP; + } else { + qDebug() << "Invalid template mode: " << mode; + QMessageBox::critical(NULL, "Error", "Invalid template mode: " + mode); + return 1; + } + } + int defaultTab = -1; if (cmdOptions.contains("tab")) { defaultTab = cmdOptions.value("tab").toInt(); diff --git a/src/session.h b/src/session.h index 8e41120..d88e766 100644 --- a/src/session.h +++ b/src/session.h @@ -4,6 +4,14 @@ class QString; class QIcon; +enum SectionType { + SECTION_NULL, + SECTION_XSESSION, + SECTION_FOR_LOCATION, + SECTION_TEMPLATES, + SECTION_GENERIC +}; + class Session { public: virtual ~Session() {} @@ -17,7 +25,7 @@ class Session { virtual QIcon icon() const = 0; virtual bool run() const = 0; virtual int type() const = 0; - virtual QString section() const = 0; + virtual SectionType section() const = 0; virtual bool operator<(const Session& s) const = 0; diff --git a/src/sessiontreeitem.cpp b/src/sessiontreeitem.cpp index bdc3edf..65deae8 100644 --- a/src/sessiontreeitem.cpp +++ b/src/sessiontreeitem.cpp @@ -1,12 +1,33 @@ #include "sessiontreeitem.h" -SessionTreeItem::SessionTreeItem(const Session* session, - SessionTreeItem *parent) - : parent_(parent), session_(session) { +QString SessionTreeItem::typeToName(const SectionType type) { + switch (type) { + case SECTION_FOR_LOCATION: + return QObject::tr("Working environments for this room"); + case SECTION_TEMPLATES: + return QObject::tr("Templates"); + case SECTION_GENERIC: + return QObject::tr("Working environments"); + case SECTION_XSESSION: + return QObject::tr("X-Sessions"); + case SECTION_NULL: + return ""; + default: + break; + } + return "Missing case for SectionType"; +} + +SessionTreeItem::SessionTreeItem(const Session* session, SessionTreeItem *parent) + : parent_(parent), session_(session), sectionType_(session->section()), text_(session->shortDescription()) { +} + +SessionTreeItem::SessionTreeItem(const SectionType type, SessionTreeItem *parent) + : parent_(parent), session_(NULL), sectionType_(type), text_(typeToName(type)) { } SessionTreeItem::SessionTreeItem(const QString& text, SessionTreeItem *parent) - : parent_(parent), session_(NULL), text_(text) { + : parent_(parent), session_(NULL), sectionType_(SECTION_NULL), text_(text) { } SessionTreeItem::~SessionTreeItem() { @@ -18,7 +39,7 @@ void SessionTreeItem::appendChild(SessionTreeItem *item) { } void SessionTreeItem::removeChild(SessionTreeItem *item) { - children_.removeOne(item); + children_.removeOne(item); } SessionTreeItem *SessionTreeItem::child(int row) { @@ -29,10 +50,6 @@ int SessionTreeItem::childCount() const { return children_.count(); } -int SessionTreeItem::columnCount() const { - return 1; -} - SessionTreeItem *SessionTreeItem::parent() { return parent_; } @@ -45,10 +62,3 @@ int SessionTreeItem::row() const { return 0; } -const Session* SessionTreeItem::session() const { - return session_; -} - -const QString SessionTreeItem::text() const { - return text_; -} diff --git a/src/sessiontreeitem.h b/src/sessiontreeitem.h index a95fb45..6202b62 100644 --- a/src/sessiontreeitem.h +++ b/src/sessiontreeitem.h @@ -4,13 +4,14 @@ #include #include -// class ... instead of include? #include "session.h" +#include "globals.h" #include class SessionTreeItem { public: SessionTreeItem(const Session* session, SessionTreeItem *parent = 0); + SessionTreeItem(const SectionType type, SessionTreeItem *parent = 0); SessionTreeItem(const QString& text, SessionTreeItem *parent = 0); ~SessionTreeItem(); @@ -19,17 +20,21 @@ class SessionTreeItem { SessionTreeItem *child(int row); int childCount() const; - int columnCount() const; + int columnCount() const { return 1; } int row() const; SessionTreeItem *parent(); - const Session* session() const; - const QString text() const; + const Session* session() const { return session_; } + const QString text() const { return text_; } + SectionType sectionType() const { return sectionType_; } private: QList children_; SessionTreeItem *parent_; const Session *session_; + const SectionType sectionType_; const QString text_; + + static QString typeToName(const SectionType type); }; #endif // SESSIONTREEITEM_H diff --git a/src/sessiontreemodel.cpp b/src/sessiontreemodel.cpp index 69c7302..d77555f 100644 --- a/src/sessiontreemodel.cpp +++ b/src/sessiontreemodel.cpp @@ -9,10 +9,11 @@ #include "sessiontreeitem.h" #include "vsession.h" #include "sessionsiconholder.h" +#include "globals.h" SessionTreeModel::SessionTreeModel(QObject *parent) : QAbstractItemModel(parent) { - root_ = new SessionTreeItem("dummy"); + root_ = new SessionTreeItem(SECTION_NULL); } SessionTreeModel::~SessionTreeModel() { @@ -129,11 +130,11 @@ void SessionTreeModel::addItems(const QList& sessions) { foreach (Session* s, sessions) { bool sectionExists = false; - QString section(s->section()); + SectionType section = s->section(); for (int i = 0; i < root_->childCount(); ++i) { SessionTreeItem* item = root_->child(i); - if (item->text() == section) { + if (item->sectionType() == section) { parentItem = item; sectionExists = true; break; @@ -192,6 +193,16 @@ QList SessionTreeModel::lookForItem(const QString& label) { } +QModelIndex SessionTreeModel::getSection(const SectionType type) { + for (int i = 0; i < root_->childCount(); ++i) { + SessionTreeItem* item = root_->child(i); + if (item->sectionType() == type && item->session() == NULL) { + return createIndex(i, 0, item); + } + } + return QModelIndex(); +} + void SessionTreeModel::updateView() { emit layoutChanged(); } diff --git a/src/sessiontreemodel.h b/src/sessiontreemodel.h index 481c5f7..6a55eed 100644 --- a/src/sessiontreemodel.h +++ b/src/sessiontreemodel.h @@ -5,8 +5,9 @@ #include #include +#include "session.h" + class SessionTreeItem; -class Session; class SessionTreeModel : public QAbstractItemModel { Q_OBJECT @@ -29,6 +30,7 @@ class SessionTreeModel : public QAbstractItemModel { void addLabelItem(const QString& label); void removeItem(const QString& name); QList lookForItem(const QString& label); + QModelIndex getSection(const SectionType type); void updateView(); diff --git a/src/vsession.cpp b/src/vsession.cpp index 79d3956..f0384a0 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -226,7 +226,14 @@ bool VSession::isValid() const { } int VSession::priority() const { - return getAttribute("priority").toInt(); + int prio = getAttribute("priority").toInt(); + if (g_templateHandling == TEMPLATES_BUMP) { + prio -= 500; + } + if (g_forLocationHandling == LOCATION_BUMP) { + prio -= 1000; + } + return prio; } void VSession::addUserAndHostname() { diff --git a/src/vsession.h b/src/vsession.h index 34c7457..49bde75 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -25,6 +25,14 @@ class VSession : public Session { bool isValid() const; int priority() const; + bool isTemplate() const { + return getAttribute("is_template").toInt() != 0; + } + + bool isForLocation() const { + return getAttribute("for_location").toInt() != 0; + } + QString shortDescription() const { return getAttribute("short_description"); } @@ -39,11 +47,14 @@ class VSession : public Session { return getAttribute("os"); } - QString section() const { - QString attr = getAttribute("section"); - if (!attr.isEmpty()) - return attr; - return QObject::tr("Priority: ").append(QString::number(priority())); + SectionType section() const { + if (g_forLocationHandling != LOCATION_IGNORE && isForLocation()) { + return SECTION_FOR_LOCATION; + } + if (g_templateHandling == TEMPLATES_BUMP && isTemplate()) { + return SECTION_TEMPLATES; + } + return SECTION_GENERIC; } QString getAttribute(const QString& nodeName, diff --git a/src/xsession.h b/src/xsession.h index 10b3b0b..5f78bff 100644 --- a/src/xsession.h +++ b/src/xsession.h @@ -19,8 +19,8 @@ class XSession : public Session { bool isValid() const; int priority() const; - QString section() const { - return QObject::tr("X-Sessions"); + SectionType section() const { + return SECTION_XSESSION; } QString shortDescription() const { -- cgit v1.2.3-55-g7522