114
by Scott Howard
Cron update. Git hash: 129d2a1 |
1 |
#include "dialogue.hpp" |
2 |
||
3 |
#include <QApplication> |
|
4 |
#include <QDesktopWidget> |
|
5 |
#include <QSplitter> |
|
6 |
#include <QListWidget> |
|
7 |
#include <QStackedWidget> |
|
8 |
#include <QListWidgetItem> |
|
9 |
||
10 |
#include "../../model/prefs/state.hpp" |
|
11 |
||
12 |
#include "page.hpp" |
|
13 |
#include "keybindingpage.hpp" |
|
117
by Scott Howard
Cron update. Git hash: 37278b5 |
14 |
|
114
by Scott Howard
Cron update. Git hash: 129d2a1 |
15 |
void CSVPrefs::Dialogue::buildCategorySelector (QSplitter *main) |
16 |
{
|
|
17 |
mList = new QListWidget (main); |
|
18 |
mList->setMinimumWidth (50); |
|
19 |
mList->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
20 |
||
21 |
mList->setSelectionBehavior (QAbstractItemView::SelectItems); |
|
22 |
||
23 |
main->addWidget (mList); |
|
24 |
||
25 |
QFontMetrics metrics (QApplication::font()); |
|
26 |
||
27 |
int maxWidth = 1; |
|
28 |
||
29 |
for (CSMPrefs::State::Iterator iter = CSMPrefs::get().begin(); iter!=CSMPrefs::get().end(); |
|
30 |
++iter) |
|
31 |
{
|
|
32 |
QString label = QString::fromUtf8 (iter->second.getKey().c_str()); |
|
33 |
maxWidth = std::max (maxWidth, metrics.width (label)); |
|
34 |
||
35 |
mList->addItem (label); |
|
36 |
}
|
|
37 |
||
38 |
mList->setMaximumWidth (maxWidth + 10); |
|
39 |
||
40 |
connect (mList, SIGNAL (currentItemChanged (QListWidgetItem *, QListWidgetItem *)), |
|
41 |
this, SLOT (selectionChanged (QListWidgetItem *, QListWidgetItem *))); |
|
42 |
}
|
|
43 |
||
44 |
void CSVPrefs::Dialogue::buildContentArea (QSplitter *main) |
|
45 |
{
|
|
46 |
mContent = new QStackedWidget (main); |
|
47 |
mContent->setSizePolicy (QSizePolicy::Preferred, QSizePolicy::Expanding); |
|
48 |
||
49 |
main->addWidget (mContent); |
|
50 |
}
|
|
51 |
||
52 |
CSVPrefs::PageBase *CSVPrefs::Dialogue::makePage (const std::string& key) |
|
53 |
{
|
|
54 |
// special case page code goes here
|
|
55 |
if (key == "Key Bindings") |
|
117
by Scott Howard
Cron update. Git hash: 37278b5 |
56 |
return new KeyBindingPage(CSMPrefs::get()[key], mContent); |
57 |
else
|
|
58 |
return new Page (CSMPrefs::get()[key], mContent); |
|
59 |
}
|
|
114
by Scott Howard
Cron update. Git hash: 129d2a1 |
60 |
|
61 |
CSVPrefs::Dialogue::Dialogue() |
|
62 |
{
|
|
63 |
setWindowTitle ("User Settings"); |
|
64 |
||
65 |
setSizePolicy (QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); |
|
66 |
||
67 |
setMinimumSize (600, 400); |
|
68 |
||
69 |
QSplitter *main = new QSplitter (this); |
|
70 |
||
71 |
setCentralWidget (main); |
|
72 |
buildCategorySelector (main); |
|
73 |
buildContentArea (main); |
|
74 |
}
|
|
75 |
||
76 |
CSVPrefs::Dialogue::~Dialogue() |
|
77 |
{
|
|
78 |
if (isVisible()) |
|
79 |
CSMPrefs::State::get().save(); |
|
80 |
}
|
|
81 |
||
82 |
void CSVPrefs::Dialogue::closeEvent (QCloseEvent *event) |
|
83 |
{
|
|
84 |
QMainWindow::closeEvent (event); |
|
85 |
||
86 |
CSMPrefs::State::get().save(); |
|
87 |
}
|
|
88 |
||
89 |
void CSVPrefs::Dialogue::show() |
|
90 |
{
|
|
91 |
if (QWidget *active = QApplication::activeWindow()) |
|
92 |
{
|
|
93 |
// place at the centre of the window with focus
|
|
94 |
QSize size = active->size(); |
|
95 |
move (active->geometry().x()+(size.width() - frameGeometry().width())/2, |
|
96 |
active->geometry().y()+(size.height() - frameGeometry().height())/2); |
|
97 |
}
|
|
98 |
else
|
|
99 |
{
|
|
100 |
// otherwise place at the centre of the screen
|
|
101 |
QPoint screenCenter = QApplication::desktop()->screenGeometry().center(); |
|
102 |
move (screenCenter - QPoint(frameGeometry().width()/2, frameGeometry().height()/2)); |
|
103 |
}
|
|
104 |
||
105 |
QWidget::show(); |
|
106 |
}
|
|
107 |
||
108 |
void CSVPrefs::Dialogue::selectionChanged (QListWidgetItem *current, QListWidgetItem *previous) |
|
109 |
{
|
|
110 |
if (current) |
|
111 |
{
|
|
112 |
std::string key = current->text().toUtf8().data(); |
|
113 |
||
114 |
for (int i=0; i<mContent->count(); ++i) |
|
115 |
{
|
|
116 |
PageBase& page = dynamic_cast<PageBase&> (*mContent->widget (i)); |
|
117 |
||
118 |
if (page.getCategory().getKey()==key) |
|
119 |
{
|
|
120 |
mContent->setCurrentIndex (i); |
|
121 |
return; |
|
122 |
}
|
|
123 |
}
|
|
124 |
||
125 |
PageBase *page = makePage (key); |
|
126 |
mContent->setCurrentIndex (mContent->addWidget (page)); |
|
127 |
}
|
|
128 |
}
|
|
129 |