From cf77e6923cae8b21b8319b7edf6102c80915b45a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 3 Jun 2019 15:47:47 +0200 Subject: Refactor user .ini class, support "recent VMs" tab, misc. cleanup --- src/dialog.cpp | 166 +++++++++++++++++++++++++++------------------------------ 1 file changed, 80 insertions(+), 86 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index 298efc4..8a9a79e 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -20,25 +20,25 @@ #include "sessiontreeitem.h" #include "globals.h" #include "vsession.h" -#include "choosersettings.h" +#include "userconfig.h" #include "filedownloader.h" static bool isProcessRunning(const QString &binary); Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { - model_[0] = new SessionTreeModel(parent); - model_[1] = new SessionTreeModel(parent); - model_[2] = new SessionTreeModel(parent); + model_[TAB_NATIVE] = new SessionTreeModel(parent); + model_[TAB_RECENT_COURSES] = new SessionTreeModel(parent); + model_[TAB_ALL_VMS] = new SessionTreeModel(parent); userInteracted_ = false; genericExpandedOnce_ = false; autoQuit_ = g_autoQuitSeconds; ui->setupUi(this); - tabs_[0] = ui->tabButtonLocal; - tabs_[1] = ui->tabButtonMyClasses; - tabs_[2] = ui->tabButtonAllClasses; + tabs_[TAB_NATIVE] = ui->tabButtonLocal; + tabs_[TAB_RECENT_COURSES] = ui->tabButtonMyClasses; + tabs_[TAB_ALL_VMS] = ui->tabButtonAllClasses; strings_[STR_LOADING] = QCoreApplication::instance()->translate("Dialog", "Loading..."); strings_[STR_URL_ERROR] = QCoreApplication::instance()->translate("Dialog", "URL Error"); @@ -84,7 +84,6 @@ Dialog::Dialog(QWidget *parent) } else { this->onTabButtonChanged(TAB_NATIVE); - this->selectPreviousSession(); } ui->chkAdminMode->setVisible(Config::isSet(Config::ALLOW_VM_EDIT)); @@ -193,8 +192,8 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) // Wait and cleanup the intermediate process waitpid(pid, nullptr, 0); } - ChooserSettings::setSetting("last-session", (s->shortDescription())); - ChooserSettings::setSetting("last-tab", QString::number(activeTab_)); + UserConfig::addLastSession(s->uuid().isEmpty() ? s->shortDescription() : s->uuid()); + UserConfig::setLastTab(activeTab_); centerTimer_->stop(); // Stop the auto-center/auto-quit timer, so we don't kill the session :> setVisible(false); } else { @@ -216,6 +215,21 @@ void Dialog::addItems(const QList& entries, int tab) { if (tab < 0 || tab > 2) { return; } + if (tab != TAB_RECENT_COURSES) { + auto prev = UserConfig::getLastSessions(); + if (!prev.isEmpty()) { + QList matches; + for (auto it : entries) { + if ((!it->uuid().isEmpty() && prev.contains(it->uuid())) + || prev.contains(it->shortDescription())) { + matches.append(it); + } + } + if (!matches.isEmpty()) { + addItems(matches, TAB_RECENT_COURSES); + } + } + } if (Config::isSet(Config::EXAM_MODE) && tab == TAB_NATIVE) return; this->model_[tab]->addItems(entries); @@ -224,30 +238,32 @@ void Dialog::addItems(const QList& entries, int tab) { setListModel(this->model_[tab]); } model_[tab]->updateView(); - selectPreviousSession(); on_filterEdit_textChanged(); + if (tab != TAB_RECENT_COURSES) { + selectPreviousSession(); + } } void Dialog::addStatusString(const int status) { if (status < 0 || status >= STR__MAX) return; - this->model_[1]->addLabelItem(strings_[status]); - this->model_[2]->addLabelItem(strings_[status]); - tabs_[1]->setEnabled(this->model_[1]->rowCount() > 1); - tabs_[2]->setEnabled(this->model_[2]->rowCount() > 1); - model_[1]->updateView(); - model_[2]->updateView(); + this->model_[TAB_RECENT_COURSES]->addLabelItem(strings_[status]); + this->model_[TAB_ALL_VMS]->addLabelItem(strings_[status]); + tabs_[TAB_RECENT_COURSES]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + tabs_[TAB_ALL_VMS]->setEnabled(this->model_[TAB_ALL_VMS]->rowCount() > 1); + model_[TAB_RECENT_COURSES]->updateView(); + model_[TAB_ALL_VMS]->updateView(); } void Dialog::removeStatusString(const int status) { if (status < 0 || status >= STR__MAX) return; - this->model_[1]->removeItem(strings_[status]); - this->model_[2]->removeItem(strings_[status]); - tabs_[1]->setEnabled(this->model_[1]->rowCount() > 1); - tabs_[2]->setEnabled(this->model_[1]->rowCount() > 1); - model_[1]->updateView(); - model_[2]->updateView(); + this->model_[TAB_RECENT_COURSES]->removeItem(strings_[status]); + this->model_[TAB_ALL_VMS]->removeItem(strings_[status]); + tabs_[TAB_RECENT_COURSES]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + tabs_[TAB_ALL_VMS]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + model_[TAB_RECENT_COURSES]->updateView(); + model_[TAB_ALL_VMS]->updateView(); } void Dialog::on_pushButtonAbort_clicked() { @@ -264,17 +280,24 @@ void Dialog::on_btnScreenSetup_clicked() { QProcess::startDetached("beamergui", QStringList("-w")); } -bool Dialog::selectSession(const QString& name) { +bool Dialog::selectSession(const QString& name, int preferredTab) { QModelIndex root(ui->treeView->rootIndex()); - for (int tab = 0; tab < TAB_COUNT; ++tab) { + int bestTab = -1; + QModelIndex bestIndex; + for (int tab = TAB_COUNT - 1; tab >= 0; --tab) { + qDebug() << "bestTab:" << bestTab << "preferred:" << preferredTab << "current:" << tab; + if (bestTab != -1 && preferredTab != tab) // We already have a potential match, only keep going if this is the desired tab + continue; + qDebug() << "checking..."; for (int i = 0; i < model_[tab]->rowCount(root); ++i) { QModelIndex section(model_[tab]->index(i, 0, root)); - if (!section.isValid()) { + qDebug() << "Section" << section << "valid:" << section.isValid(); + if (!section.isValid()) continue; - } for (int j = 0; j < model_[tab]->rowCount(section); ++j) { QModelIndex index(model_[tab]->index(j, 0, section)); + qDebug() << "Item" << index << "valid:" << index.isValid(); if (!index.isValid()) continue; SessionTreeItem* item = static_cast(index.internalPointer()); @@ -282,59 +305,27 @@ bool Dialog::selectSession(const QString& name) { if (s == nullptr) { continue; } - if (s->shortDescription() == name) { - // change the tab - onTabButtonChanged(tab); - // set selection - ui->treeView->selectionModel()->clearSelection(); - ui->treeView->selectionModel()->clear(); - ui->treeView->selectionModel()->select(index, QItemSelectionModel::Select); - ui->treeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select); - ui->treeView->scrollTo(index); - return true; + qDebug() << "Checking session" << s->shortDescription(); + if ((!s->uuid().isEmpty() && s->uuid() == name) || s->shortDescription() == name) { + bestTab = tab; + bestIndex = index; + break; // Break inner, keep checking other tabs } } } } - return false; -} - -bool Dialog::selectSessionByUuid(const QString& name) { - QModelIndex root(ui->treeView->rootIndex()); - - for (int tab = 0; tab < TAB_COUNT; ++tab) { - for (int i = 0; i < model_[tab]->rowCount(root); ++i) { - QModelIndex section(model_[tab]->index(i, 0, root)); - if (!section.isValid()) { - break; - } - for (int j = 0; j < model_[tab]->rowCount(section); ++j) { - QModelIndex index(model_[tab]->index(j, 0, section)); - SessionTreeItem* item = static_cast(index.internalPointer()); - const Session* s(item->session()); - if (s == nullptr) { - continue; - } - /* TODO: implement this here */ - /* check if it is a vsession, then access the uuid and compare with name*/ - if (s->type() == Session::VSESSION) { - /* cast to vsession */ - const VSession* v = static_cast(s); - if (v->uuid() == name) { - // change the tab - onTabButtonChanged(tab); - // set selection - ui->treeView->selectionModel()->clearSelection(); - ui->treeView->selectionModel()->clear(); - ui->treeView->selectionModel()->select(index, QItemSelectionModel::Select); - ui->treeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select); - ui->treeView->scrollTo(index); - return true; - } - } - } - } + if (bestTab != -1) { + // change the tab + qDebug() << "Best tab is" << bestTab; + onTabButtonChanged(bestTab); + // set selection + ui->treeView->selectionModel()->clearSelection(); + ui->treeView->selectionModel()->clear(); + ui->treeView->selectionModel()->select(bestIndex, QItemSelectionModel::Select); + ui->treeView->selectionModel()->setCurrentIndex(bestIndex, QItemSelectionModel::Select); + ui->treeView->scrollTo(bestIndex); + return true; } return false; @@ -345,24 +336,27 @@ void Dialog::selectPreviousSession() { qDebug() << "Not selecting previous session as user interacted or session was already selected"; return; } + int lastTab = UserConfig::getLastTab(); QString lastSession = Config::get(Config::DEFAULT_SESSION); if (lastSession.isEmpty()) { - lastSession = ChooserSettings::getSetting("last-session"); + auto list = UserConfig::getLastSessions(); + if (!list.isEmpty()) { + lastSession = list.back(); + } } if (!lastSession.isEmpty()) { qDebug() << "Trying to select last session: " << lastSession; ui->treeView->clearSelection(); - if (selectSession(lastSession)) { + if (selectSession(lastSession, lastTab)) { qDebug() << "Success"; userInteracted_ = true; return; } } // could not find last session, change to last used tab - QString lastTab = ChooserSettings::getSetting("last-tab"); - if (!lastTab.isEmpty()) { + if (lastTab >= 0 && lastTab < TAB_COUNT) { qDebug() << "Trying to select last tab " << lastTab; - this->onTabButtonChanged(lastTab.toInt()); + this->onTabButtonChanged(lastTab); } else { int defaultTab = Config::get(Config::DEFAULT_TAB).toInt(); qDebug() << "Selected default tab " << defaultTab; @@ -561,13 +555,13 @@ void Dialog::downloadData(const QString& locationIds) { + infoNode.text() + "

")); } - if (ChooserSettings::getSetting("last-news").toUInt() < timestamp.toTime_t()) { + if (UserConfig::getLastNewsTime() < timestamp.toTime_t()) { // show news if not seen before on_helpNewsButton_clicked(); } // update ini - ChooserSettings::setSetting("last-news", QString::number(timestamp.toTime_t())); + UserConfig::setLastNewsTime(timestamp.toTime_t()); // make backup QFile file(TEMP_PATH_NEWS); @@ -713,7 +707,7 @@ void Dialog::on_tabButtonLocal_clicked() { void Dialog::on_tabButtonMyClasses_clicked() { userInteracted_ = true; - onTabButtonChanged(TAB_MY_COURSES); + onTabButtonChanged(TAB_RECENT_COURSES); } void Dialog::on_tabButtonAllClasses_clicked() { @@ -776,7 +770,7 @@ void Dialog::on_filterEdit_textChanged() { void Dialog::setListModel(SessionTreeModel *model) { QAbstractItemModel *old = nullptr; - if (ui->treeView->model() == model_[0] || ui->treeView->model() == model_[1] || ui->treeView->model() == model_[2]) { + if (ui->treeView->model() == model_[TAB_NATIVE] || ui->treeView->model() == model_[TAB_RECENT_COURSES] || ui->treeView->model() == model_[TAB_ALL_VMS]) { } else { old = ui->treeView->model(); } @@ -830,7 +824,7 @@ void Dialog::keyPressEvent(QKeyEvent* event) { void Dialog::iconDownloaded(const QUrl& url, const QIcon&) { qDebug() << "Icon downloaded... (" << url << ")"; // TODO: Check which model(s) contain an entry with this icon - model_[TAB_MY_COURSES]->updateView(); + model_[TAB_RECENT_COURSES]->updateView(); model_[TAB_ALL_VMS]->updateView(); } @@ -903,7 +897,7 @@ bool Dialog::eventFilter(QObject*, QEvent *event) { } void Dialog::checkAutostart() { if (!autoStartEntry_.isEmpty()) { - if (this->selectSessionByUuid(autoStartEntry_) || this->selectSession(autoStartEntry_)) { + if (this->selectSession(autoStartEntry_)) { this->on_treeView_doubleClicked(ui->treeView->selectionModel()->currentIndex()); } else { QMessageBox::critical(this, "Autostart", QString::fromUtf8("Konnte %1 nicht starten.").arg(autoStartEntry_)); -- cgit v1.2.3-55-g7522