libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / kamera / kcontrol / kameraconfigdialog.cpp
blob1667779687cd001a428a39261d82be012f044cd0
1 /*
3 Copyright (C) 2001 The Kompany
4 2002-2003 Ilya Konstantinov <kde-devel@future.shiny.co.il>
5 2002-2003 Marcus Meissner <marcus@jet.franken.de>
6 2003 Nadeem Hasan <nhasan@nadmm.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <qlayout.h>
24 #include <q3grid.h>
25 #include <qlabel.h>
26 #include <Q3GroupBox>
27 #include <qcheckbox.h>
28 #include <qradiobutton.h>
29 #include <qlineedit.h>
30 #include <qcombobox.h>
31 #include <qslider.h>
32 #include <Q3ButtonGroup>
34 #include <kvbox.h>
35 #include <qtabwidget.h>
37 //Added by qt3to4:
38 #include <QFrame>
39 #include <QVBoxLayout>
41 #include <klocale.h>
43 #include "kameraconfigdialog.h"
44 #include "kameraconfigdialog.moc"
46 KameraConfigDialog::KameraConfigDialog(Camera */*camera*/,
47 CameraWidget *widget,
48 QWidget *parent,
49 const char *name) :
50 KDialog(parent),
51 m_widgetRoot(widget)
53 setButtons( Ok|Cancel );
54 setDefaultButton( Ok );
55 setModal( true );
57 QFrame *main = new QFrame( this );
58 setMainWidget( main );
59 QVBoxLayout *topLayout = new QVBoxLayout(main);
60 topLayout->setSpacing(spacingHint());
61 topLayout->setMargin(0);
63 m_tabWidget = 0;
65 appendWidget(main, widget);
66 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
69 void KameraConfigDialog::appendWidget(QWidget *parent, CameraWidget *widget)
71 QWidget *newParent = parent;
73 CameraWidgetType widget_type;
74 const char *widget_name;
75 const char *widget_info;
76 const char *widget_label;
77 float widget_value_float;
78 int widget_value_int;
79 const char *widget_value_string;
80 gp_widget_get_type(widget, &widget_type);
81 gp_widget_get_label(widget, &widget_label);
82 gp_widget_get_info(widget, &widget_info);
83 gp_widget_get_name(widget, &widget_name);
85 QString whats_this = QString::fromLocal8Bit(widget_info); // gphoto2 doesn't seem to have any standard for i18n
87 // Add this widget to parent
88 switch(widget_type) {
89 case GP_WIDGET_WINDOW:
91 setCaption(widget_label);
93 break;
95 case GP_WIDGET_SECTION:
97 if (!m_tabWidget) {
98 m_tabWidget = new QTabWidget(parent);
99 parent->layout()->addWidget(m_tabWidget);
101 QWidget *tab = new QWidget(m_tabWidget);
102 // widgets are to be aligned vertically in the tab
103 QVBoxLayout *tabLayout = new QVBoxLayout(tab, marginHint(),
104 spacingHint());
105 m_tabWidget->insertTab(tab, widget_label);
106 KVBox *tabContainer = new KVBox(tab);
107 tabContainer->setSpacing(spacingHint());
108 tabLayout->addWidget(tabContainer);
109 newParent = tabContainer;
111 tabLayout->addStretch();
113 break;
115 case GP_WIDGET_TEXT:
117 gp_widget_get_value(widget, &widget_value_string);
119 Q3Grid *grid = new Q3Grid(2, Qt::Horizontal, parent);
120 parent->layout()->addWidget(grid);
121 grid->setSpacing(spacingHint());
122 new QLabel(QString::fromLocal8Bit( widget_label )+':', grid);
123 QLineEdit *lineEdit = new QLineEdit(widget_value_string, grid);
124 m_wmap.insert(widget, lineEdit);
126 if (!whats_this.isEmpty())
127 grid->setWhatsThis( whats_this);
129 break;
131 case GP_WIDGET_RANGE:
133 float widget_low;
134 float widget_high;
135 float widget_increment;
136 gp_widget_get_range(widget, &widget_low, &widget_high, &widget_increment);
137 gp_widget_get_value(widget, &widget_value_float);
139 Q3GroupBox *groupBox = new Q3GroupBox(1, Qt::Horizontal,widget_label, parent);
140 parent->layout()->addWidget(groupBox);
141 QSlider *slider = new QSlider(
142 ( int )widget_low,
143 ( int )widget_high,
144 ( int )widget_increment,
145 ( int )widget_value_float,
146 Qt::Horizontal,
147 groupBox );
148 m_wmap.insert(widget, slider);
150 if (!whats_this.isEmpty())
151 groupBox->setWhatsThis( whats_this);
153 break;
155 case GP_WIDGET_TOGGLE:
157 gp_widget_get_value(widget, &widget_value_int);
159 QCheckBox *checkBox = new QCheckBox(widget_label, parent);
160 parent->layout()->addWidget(checkBox);
161 checkBox->setChecked(widget_value_int);
162 m_wmap.insert(widget, checkBox);
164 if (!whats_this.isEmpty())
165 checkBox->setWhatsThis( whats_this);
167 break;
169 case GP_WIDGET_RADIO:
171 gp_widget_get_value(widget, &widget_value_string);
173 int count = gp_widget_count_choices(widget);
175 // for less than 5 options, align them horizontally
176 Q3ButtonGroup *buttonGroup;
177 if (count > 4)
178 buttonGroup = new Q3VButtonGroup(widget_label, parent);
179 else
180 buttonGroup = new Q3HButtonGroup(widget_label, parent);
181 parent->layout()->addWidget(buttonGroup);
183 for(int i = 0; i < count; ++i) {
184 const char *widget_choice;
185 gp_widget_get_choice(widget, i, &widget_choice);
187 new QRadioButton(widget_choice, buttonGroup);
188 if(!strcmp(widget_value_string, widget_choice))
189 buttonGroup->setButton(i);
191 m_wmap.insert(widget, buttonGroup);
193 if (!whats_this.isEmpty())
194 buttonGroup->setWhatsThis( whats_this);
196 break;
198 case GP_WIDGET_MENU:
200 gp_widget_get_value(widget, &widget_value_string);
202 QComboBox *comboBox = new QComboBox(parent);
203 parent->layout()->addWidget(comboBox);
204 comboBox->clear();
205 for(int i = 0; i < gp_widget_count_choices(widget); ++i) {
206 const char *widget_choice;
207 gp_widget_get_choice(widget, i, &widget_choice);
209 comboBox->insertItem(widget_choice);
210 if(!strcmp(widget_value_string, widget_choice))
211 comboBox->setCurrentItem(i);
213 m_wmap.insert(widget, comboBox);
215 if (!whats_this.isEmpty())
216 comboBox->setWhatsThis( whats_this);
218 break;
220 case GP_WIDGET_BUTTON:
222 // TODO
223 // I can't see a way of implementing this. Since there is
224 // no way of telling which button sent you a signal, we
225 // can't map to the appropriate widget->callback
226 QLabel *label = new QLabel(i18n("Button (not supported by KControl)"), parent);
227 parent->layout()->addWidget(label);
229 break;
231 case GP_WIDGET_DATE:
233 // TODO
234 QLabel * label = new QLabel(i18n("Date (not supported by KControl)"), parent);
235 parent->layout()->addWidget(label);
237 break;
239 default:
240 return;
243 // Append all this widgets children
244 for(int i = 0; i < gp_widget_count_children(widget); ++i) {
245 CameraWidget *widget_child;
246 gp_widget_get_child(widget, i, &widget_child);
247 appendWidget(newParent, widget_child);
250 // Things that must be done after all children were added
252 switch (widget_type) {
253 case GP_WIDGET_SECTION:
255 tabLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding) );
256 break;
262 void KameraConfigDialog::updateWidgetValue(CameraWidget *widget)
264 CameraWidgetType widget_type;
265 gp_widget_get_type(widget, &widget_type);
267 switch(widget_type) {
268 case GP_WIDGET_WINDOW:
269 // nothing to do
270 break;
271 case GP_WIDGET_SECTION:
272 // nothing to do
273 break;
274 case GP_WIDGET_TEXT:
276 QLineEdit *lineEdit = static_cast<QLineEdit *>(m_wmap[widget]);
277 gp_widget_set_value(widget, (void *)lineEdit->text().toLocal8Bit().data());
279 break;
281 case GP_WIDGET_RANGE:
283 QSlider *slider = static_cast<QSlider *>(m_wmap[widget]);
284 float value_float = slider->value();
285 gp_widget_set_value(widget, (void *)&value_float);
287 break;
289 case GP_WIDGET_TOGGLE:
291 QCheckBox *checkBox = static_cast<QCheckBox *>(m_wmap[widget]);
292 int value_int = checkBox->isChecked() ? 1 : 0;
293 gp_widget_set_value(widget, (void *)&value_int);
295 break;
297 case GP_WIDGET_RADIO:
299 Q3ButtonGroup *buttonGroup = static_cast<Q3VButtonGroup *>(m_wmap[widget]);
300 gp_widget_set_value(widget, (void *)buttonGroup->selected()->text().toLocal8Bit().data());
302 break;
304 case GP_WIDGET_MENU:
306 QComboBox *comboBox = static_cast<QComboBox *>(m_wmap[widget]);
307 gp_widget_set_value(widget, (void *)comboBox->currentText().toLocal8Bit().data());
309 break;
311 case GP_WIDGET_BUTTON:
312 // nothing to do
313 break;
314 case GP_WIDGET_DATE:
316 // not implemented
317 break;
321 // Copy child widget values
322 for(int i = 0; i < gp_widget_count_children(widget); ++i) {
323 CameraWidget *widget_child;
324 gp_widget_get_child(widget, i, &widget_child);
325 updateWidgetValue(widget_child);
329 void KameraConfigDialog::slotOk()
331 // Copy Qt widget values into CameraWidget hierarchy
332 updateWidgetValue(m_widgetRoot);
334 // 'ok' dialog
335 accept();