some settings refactorings

This commit is contained in:
fourtf 2020-02-21 01:17:22 +01:00
parent fbbf34878f
commit 39d40d6db6
4 changed files with 52 additions and 17 deletions

View file

@ -4,7 +4,6 @@
#include "singletons/Resources.hpp"
#include "util/LayoutCreator.hpp"
#include "widgets/helper/Button.hpp"
#include "widgets/helper/SettingsDialogTab.hpp"
#include "widgets/settingspages/AboutPage.hpp"
#include "widgets/settingspages/AccountsPage.hpp"
#include "widgets/settingspages/CommandPage.hpp"
@ -21,7 +20,7 @@
namespace chatterino {
SettingsDialog *SettingsDialog::handle = nullptr;
SettingsDialog *SettingsDialog::instance_ = nullptr;
SettingsDialog::SettingsDialog()
: BaseWindow(BaseWindow::DisableCustomScaling)
@ -105,10 +104,10 @@ void SettingsDialog::initUi()
void SettingsDialog::filterElements(const QString &text)
{
// filter elements and hide pages
for (auto &&page : this->pages_)
for (auto &&tab : this->tabs_)
{
// filterElements returns true if anything on the page matches the search query
page->tab()->setVisible(page->filterElements(text));
tab->setVisible(tab->page()->filterElements(text));
}
// find next visible page
@ -146,9 +145,9 @@ void SettingsDialog::filterElements(const QString &text)
}
}
SettingsDialog *SettingsDialog::getHandle()
SettingsDialog *SettingsDialog::instance()
{
return SettingsDialog::handle;
return SettingsDialog::instance_;
}
void SettingsDialog::addTabs()
@ -173,7 +172,7 @@ void SettingsDialog::addTabs()
this->ui_.tabContainer->addSpacing(16);
this->addTab(new KeyboardSettingsPage);
this->addTab(this->ui_.moderationPage = new ModerationPage);
this->addTab(new ModerationPage);
this->addTab(new NotificationPage);
this->addTab(new ExternalToolsPage);
@ -189,7 +188,6 @@ void SettingsDialog::addTab(SettingsPage *page, Qt::Alignment alignment)
this->ui_.pageStack->addWidget(page);
this->ui_.tabContainer->addWidget(tab, 0, alignment);
this->tabs_.push_back(tab);
this->pages_.push_back(page);
if (this->tabs_.size() == 1)
{
@ -217,6 +215,21 @@ void SettingsDialog::selectTab(SettingsDialogTab *tab, bool byUser)
}
}
void SettingsDialog::selectTab(SettingsTabId id)
{
this->selectTab(this->tab(id));
}
SettingsDialogTab *SettingsDialog::tab(SettingsTabId id)
{
for (auto &&tab : this->tabs_)
if (tab->id() == id)
return tab;
assert(false);
return nullptr;
}
void SettingsDialog::selectPage(SettingsPage *page)
{
assert(page);
@ -233,12 +246,18 @@ void SettingsDialog::showDialog(SettingsDialogPreference preferredTab)
switch (preferredTab)
{
case SettingsDialogPreference::Accounts:
instance->selectTab(instance->tabs_.at(1));
instance->selectTab(SettingsTabId::Accounts);
break;
case SettingsDialogPreference::ModerationActions:
instance->selectPage(instance->ui_.moderationPage);
instance->ui_.moderationPage->selectModerationActions();
if (auto tab = instance->tab(SettingsTabId::Moderation))
{
instance->selectTab(tab);
if (auto page = dynamic_cast<ModerationPage *>(tab->page()))
{
page->selectModerationActions();
}
}
break;
default:;

View file

@ -7,6 +7,7 @@
#include <QVBoxLayout>
#include <QWidget>
#include <pajlada/settings/setting.hpp>
#include "widgets/helper/SettingsDialogTab.hpp"
class QLineEdit;
@ -32,7 +33,7 @@ class SettingsDialog : public BaseWindow
public:
SettingsDialog();
static SettingsDialog *getHandle(); // may be NULL
static SettingsDialog *instance(); // may be NULL
static void showDialog(SettingsDialogPreference preferredTab =
SettingsDialogPreference::NoPreference);
@ -42,7 +43,7 @@ protected:
virtual void showEvent(QShowEvent *) override;
private:
static SettingsDialog *handle;
static SettingsDialog *instance_;
void refresh();
@ -51,6 +52,8 @@ private:
void addTab(SettingsPage *page, Qt::Alignment alignment = Qt::AlignTop);
void selectTab(SettingsDialogTab *tab, bool byUser = true);
void selectPage(SettingsPage *page);
void selectTab(SettingsTabId id);
SettingsDialogTab *tab(SettingsTabId id);
void filterElements(const QString &query);
void onOkClicked();
@ -62,11 +65,9 @@ private:
QStackedLayout *pageStack{};
QPushButton *okButton{};
QPushButton *cancelButton{};
ModerationPage *moderationPage{};
QLineEdit *search{};
} ui_;
std::vector<SettingsDialogTab *> tabs_;
std::vector<SettingsPage *> pages_;
SettingsDialogTab *selectedTab_{};
SettingsDialogTab *lastSelectedByUser_{};

View file

@ -8,10 +8,12 @@
namespace chatterino {
SettingsDialogTab::SettingsDialogTab(SettingsDialog *_dialog,
SettingsPage *_page, QString imageFileName)
SettingsPage *_page, QString imageFileName,
SettingsTabId id)
: BaseWidget(_dialog)
, dialog_(_dialog)
, page_(_page)
, id_(id)
{
this->ui_.labelText = page_->getName();
this->ui_.icon.addFile(imageFileName);
@ -72,4 +74,9 @@ void SettingsDialogTab::mousePressEvent(QMouseEvent *event)
this->setFocus();
}
SettingsTabId SettingsDialogTab::id() const
{
return id_;
}
} // namespace chatterino

View file

@ -11,16 +11,23 @@ namespace chatterino {
class SettingsPage;
class SettingsDialog;
enum SettingsTabId {
None,
Accounts,
Moderation,
};
class SettingsDialogTab : public BaseWidget
{
Q_OBJECT
public:
SettingsDialogTab(SettingsDialog *dialog_, SettingsPage *page_,
QString imageFileName);
QString imageFileName, SettingsTabId id = {});
void setSelected(bool selected_);
SettingsPage *page();
SettingsTabId id() const;
signals:
void selectedChanged(bool);
@ -37,6 +44,7 @@ private:
// Parent settings dialog
SettingsDialog *dialog_;
SettingsPage *page_;
SettingsTabId id_;
bool selected_ = false;
};