]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconsviewsettingspage.cpp
don't overrun the array (CID 3278)
[dolphin.git] / src / iconsviewsettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "iconsviewsettingspage.h"
21
22 #include "dolphinsettings.h"
23 #include "iconsizedialog.h"
24 #include "pixmapviewer.h"
25
26 #include "dolphin_iconsmodesettings.h"
27
28 #include <qlabel.h>
29 #include <qslider.h>
30 #include <q3buttongroup.h>
31 #include <qradiobutton.h>
32 #include <qspinbox.h>
33 #include <qfontcombobox.h>
34
35 #include <kfontrequester.h>
36 #include <kiconloader.h>
37 #include <kdialog.h>
38 #include <kglobalsettings.h>
39 #include <klocale.h>
40 #include <kvbox.h>
41
42 #include <QPushButton>
43 #include <QListView>
44
45 #define GRID_SPACING_BASE 8
46 #define GRID_SPACING_INC 24
47
48 IconsViewSettingsPage::IconsViewSettingsPage(DolphinMainWindow* mainWindow,
49 QWidget* parent) :
50 KVBox(parent),
51 m_mainWindow(mainWindow),
52 m_iconSize(0),
53 m_previewSize(0),
54 m_iconSizeButton(0),
55 m_textWidthBox(0),
56 m_fontRequester(0),
57 m_textlinesCountBox(0),
58 m_arrangementBox(0),
59 m_gridSpacingBox(0)
60 {
61 const int spacing = KDialog::spacingHint();
62 const int margin = KDialog::marginHint();
63 const QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
64
65 setSpacing(spacing);
66 setMargin(margin);
67
68 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
69 Q_ASSERT(settings != 0);
70 m_iconSize = settings->iconSize();
71 m_previewSize = settings->previewSize();
72
73 KHBox* sizesLayout = new KHBox(this);
74 sizesLayout->setSpacing(spacing);
75 sizesLayout->setSizePolicy(sizePolicy);
76
77 m_iconSizeButton = new QPushButton(i18n("Change icon and preview size..."), this);
78 connect(m_iconSizeButton, SIGNAL(clicked()),
79 this, SLOT(openIconSizeDialog()));
80
81 Q3GroupBox* textGroup = new Q3GroupBox(2, Qt::Horizontal, i18n("Text"), this);
82 textGroup->setSizePolicy(sizePolicy);
83 textGroup->setMargin(margin);
84
85 new QLabel(i18n("Font:"), textGroup);
86 m_fontRequester = new KFontRequester(textGroup);
87 QFont font(settings->fontFamily(),
88 settings->fontSize());
89 font.setItalic(settings->italicFont());
90 font.setBold(settings->boldFont());
91 m_fontRequester->setFont(font);
92
93 new QLabel(i18n("Number of lines:"), textGroup);
94 m_textlinesCountBox = new QSpinBox(1, 5, 1, textGroup);
95 m_textlinesCountBox->setValue(settings->numberOfTextlines());
96
97 new QLabel(i18n("Text width:"), textGroup);
98 m_textWidthBox = new QComboBox(textGroup);
99 m_textWidthBox->addItem(i18n("Small"));
100 m_textWidthBox->addItem(i18n("Medium"));
101 m_textWidthBox->addItem(i18n("Large"));
102
103 Q3GroupBox* gridGroup = new Q3GroupBox(2, Qt::Horizontal, i18n("Grid"), this);
104 gridGroup->setSizePolicy(sizePolicy);
105 gridGroup->setMargin(margin);
106
107 const bool leftToRightArrangement = (settings->arrangement() == QListView::LeftToRight);
108 new QLabel(i18n("Arrangement:"), gridGroup);
109 m_arrangementBox = new QComboBox(gridGroup);
110 m_arrangementBox->addItem(i18n("Left to right"));
111 m_arrangementBox->addItem(i18n("Top to bottom"));
112 m_arrangementBox->setCurrentIndex(leftToRightArrangement ? 0 : 1);
113
114 new QLabel(i18n("Grid spacing:"), gridGroup);
115 m_gridSpacingBox = new QComboBox(gridGroup);
116 m_gridSpacingBox->addItem(i18n("Small"));
117 m_gridSpacingBox->addItem(i18n("Medium"));
118 m_gridSpacingBox->addItem(i18n("Large"));
119 m_gridSpacingBox->setCurrentIndex((settings->gridSpacing() - GRID_SPACING_BASE) / GRID_SPACING_INC);
120
121 // Add a dummy widget with no restriction regarding
122 // a vertical resizing. This assures that the dialog layout
123 // is not stretched vertically.
124 new QWidget(this);
125
126 adjustTextWidthSelection();
127 }
128
129 IconsViewSettingsPage::~IconsViewSettingsPage()
130 {
131 }
132
133 void IconsViewSettingsPage::applySettings()
134 {
135 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
136 Q_ASSERT(settings != 0);
137
138 settings->setIconSize(m_iconSize);
139 settings->setPreviewSize(m_previewSize);
140
141 const QFont font = m_fontRequester->font();
142 const int fontSize = font.pointSize();
143
144 const int arrangement = (m_arrangementBox->currentIndex() == 0) ?
145 QListView::LeftToRight :
146 QListView::TopToBottom;
147
148 settings->setArrangement(arrangement);
149
150 // TODO: this is just a very rough testing code to calculate the grid
151 // width and height
152 const int defaultSize = settings->iconSize();
153 int gridWidth = defaultSize;
154 int gridHeight = defaultSize;
155 const int textSizeIndex = m_textWidthBox->currentIndex();
156 if (arrangement == QListView::TopToBottom) {
157 gridWidth += 96 + textSizeIndex * 32;
158 gridHeight += 64;
159 }
160 else {
161 gridWidth += 128 + textSizeIndex * 64;
162 }
163
164 settings->setGridWidth(gridWidth);
165 settings->setGridHeight(gridHeight);
166
167 settings->setFontFamily(font.family());
168 settings->setFontSize(fontSize);
169 settings->setItalicFont(font.italic());
170 settings->setBoldFont(font.bold());
171
172 settings->setNumberOfTextlines(m_textlinesCountBox->value());
173
174 settings->setGridSpacing(GRID_SPACING_BASE +
175 m_gridSpacingBox->currentIndex() * GRID_SPACING_INC);
176 }
177
178 void IconsViewSettingsPage::openIconSizeDialog()
179 {
180 IconSizeDialog dialog(this);
181 if (dialog.exec() == QDialog::Accepted) {
182 m_iconSize = dialog.iconSize();
183 m_previewSize = dialog.previewSize();
184 }
185 }
186
187
188
189 void IconsViewSettingsPage::adjustTextWidthSelection()
190 {
191 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
192 Q_ASSERT(settings != 0);
193 //m_textWidthBox->setCurrentIndex(DolphinSettings::instance().textWidthHint());
194 }
195
196 #include "iconsviewsettingspage.moc"