1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 19:53:54 +00:00
openmw/apps/opencs/view/prefs/dialogue.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
3.7 KiB
C++
Raw Normal View History

#include "dialogue.hpp"
2022-10-19 17:02:00 +00:00
#include <exception>
#include <map>
#include <utility>
#include <QApplication>
2015-12-06 11:06:28 +00:00
#include <QListWidgetItem>
#include <QScreen>
#include <QSplitter>
#include <QStackedWidget>
2018-11-13 19:07:01 +00:00
#include <components/debug/debuglog.hpp>
2022-10-19 17:02:00 +00:00
#include <apps/opencs/model/prefs/category.hpp>
#include <apps/opencs/view/prefs/pagebase.hpp>
#include "../../model/prefs/state.hpp"
#include "contextmenulist.hpp"
2016-07-27 23:15:24 +00:00
#include "keybindingpage.hpp"
2015-12-08 16:21:58 +00:00
#include "page.hpp"
2015-12-08 11:04:45 +00:00
void CSVPrefs::Dialogue::buildCategorySelector(QSplitter* main)
{
CSVPrefs::ContextMenuList* list = new CSVPrefs::ContextMenuList(main);
list->setMinimumWidth(50);
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
list->setSelectionBehavior(QAbstractItemView::SelectItems);
main->addWidget(list);
2017-09-17 09:09:28 +00:00
QFontMetrics metrics(QApplication::font(list));
2015-12-06 11:06:28 +00:00
int maxWidth = 1;
2015-12-08 08:56:42 +00:00
for (CSMPrefs::State::Iterator iter = CSMPrefs::get().begin(); iter != CSMPrefs::get().end(); ++iter)
2015-12-06 11:06:28 +00:00
{
2015-12-08 08:56:42 +00:00
QString label = QString::fromUtf8(iter->second.getKey().c_str());
maxWidth = std::max(maxWidth, metrics.horizontalAdvance(label));
2015-12-06 11:06:28 +00:00
list->addItem(label);
2015-12-06 11:06:28 +00:00
}
list->setMaximumWidth(maxWidth + 10);
2015-12-06 11:06:28 +00:00
connect(list, &ContextMenuList::currentItemChanged, this, &Dialogue::selectionChanged);
}
void CSVPrefs::Dialogue::buildContentArea(QSplitter* main)
{
mContent = new QStackedWidget(main);
mContent->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
main->addWidget(mContent);
}
2015-12-08 16:21:58 +00:00
CSVPrefs::PageBase* CSVPrefs::Dialogue::makePage(const std::string& key)
{
// special case page code goes here
2016-07-27 23:15:24 +00:00
if (key == "Key Bindings")
return new KeyBindingPage(CSMPrefs::get()[key], mContent);
else
return new Page(CSMPrefs::get()[key], mContent);
2015-12-08 16:21:58 +00:00
}
2015-12-08 08:56:42 +00:00
CSVPrefs::Dialogue::Dialogue()
{
setWindowTitle("User Settings");
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setMinimumSize(600, 400);
QSplitter* main = new QSplitter(this);
setCentralWidget(main);
buildCategorySelector(main);
buildContentArea(main);
}
CSVPrefs::Dialogue::~Dialogue()
{
2018-11-13 19:07:01 +00:00
try
{
if (isVisible())
CSMPrefs::State::get().save();
}
catch (const std::exception& e)
{
Log(Debug::Error) << "Error in the destructor: " << e.what();
}
}
void CSVPrefs::Dialogue::closeEvent(QCloseEvent* event)
{
QMainWindow::closeEvent(event);
CSMPrefs::State::get().save();
}
void CSVPrefs::Dialogue::show()
{
if (QWidget* active = QApplication::activeWindow())
{
// place at the centre of the window with focus
QSize size = active->size();
move(active->geometry().x() + (size.width() - frameGeometry().width()) / 2,
active->geometry().y() + (size.height() - frameGeometry().height()) / 2);
}
else
{
QRect scr = QGuiApplication::primaryScreen()->geometry();
// otherwise place at the centre of the screen
QPoint screenCenter = scr.center();
move(screenCenter - QPoint(frameGeometry().width() / 2, frameGeometry().height() / 2));
}
QWidget::show();
}
2015-12-08 11:04:45 +00:00
void CSVPrefs::Dialogue::selectionChanged(QListWidgetItem* current, QListWidgetItem* previous)
{
if (current)
{
std::string key = current->text().toUtf8().data();
for (int i = 0; i < mContent->count(); ++i)
{
PageBase& page = dynamic_cast<PageBase&>(*mContent->widget(i));
if (page.getCategory().getKey() == key)
{
mContent->setCurrentIndex(i);
return;
}
}
2015-12-08 16:21:58 +00:00
PageBase* page = makePage(key);
2015-12-08 11:04:45 +00:00
mContent->setCurrentIndex(mContent->addWidget(page));
}
}