Permalink
Browse files

converted to qt4

  • Loading branch information...
1 parent 079b3b3 commit 827306b7d1cc1e65552e0b476ddc0ac74b5c304e @icefox committed Oct 20, 2005
Showing with 705 additions and 328 deletions.
  1. +0 −10 example/application.pro
  2. +206 −0 example/general.ui
  3. +68 −0 example/mainwindow.cpp
  4. +13 −2 example/mainwindow.h
  5. +104 −0 example/other.ui
  6. +2 −0 qautoconfig.pro
  7. +0 −293 src/kautoconfig.h
  8. +0 −16 src/kconfig.cpp
  9. +16 −7 src/qautoconfig.cpp
  10. +291 −0 src/qautoconfig.h
  11. +5 −0 src/qautoconfig.pri
View
10 example/application.pro
@@ -1,10 +0,0 @@
-HEADERS = mainwindow.h
-SOURCES = main.cpp \
- mainwindow.cpp
-RESOURCES = application.qrc
-
-# install
-target.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/application
-sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS application.pro images
-sources.path = $$[QT_INSTALL_EXAMPLES]/mainwindows/application
-INSTALLS += target sources
View
206 example/general.ui
@@ -0,0 +1,206 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>General</class>
+ <widget class="QWidget" name="General" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>453</width>
+ <height>375</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Form2</string>
+ </property>
+ <layout class="QGridLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item row="2" column="1" >
+ <widget class="QLabel" name="textLabel1" >
+ <property name="text" >
+ <string>Type:</string>
+ </property>
+ </widget>
+ </item>
+ <item rowspan="2" row="1" column="0" >
+ <widget class="Q3ButtonGroup" name="modal" >
+ <property name="title" >
+ <string>Modal</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>0</number>
+ </property>
+ <item>
+ <widget class="QRadioButton" name="modalFalse" >
+ <property name="text" >
+ <string>False</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QRadioButton" name="modalTrue" >
+ <property name="text" >
+ <string>True</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item row="1" column="2" >
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeType" >
+ <enum>QSizePolicy::Expanding</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>20</width>
+ <height>31</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item row="4" column="0" colspan="3" >
+ <widget class="QSpinBox" name="spinBox1" />
+ </item>
+ <item row="5" column="1" >
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeType" >
+ <enum>QSizePolicy::Expanding</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>20</width>
+ <height>16</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item row="0" column="0" colspan="3" >
+ <widget class="QLabel" name="info" >
+ <property name="text" >
+ <string>These settings will apply the next time the configure dialog is created.</string>
+ </property>
+ <property name="alignment" >
+ <set>Qt::AlignVCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item row="2" column="2" >
+ <widget class="Q3ComboBox" name="type" />
+ </item>
+ <item row="3" column="0" colspan="3" >
+ <widget class="Q3GroupBox" name="groupBox1" >
+ <property name="title" >
+ <string>Buttons</string>
+ </property>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>11</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QCheckBox" name="helpbutton" >
+ <property name="text" >
+ <string>Help</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="defaultsbutton" >
+ <property name="text" >
+ <string>Defaults</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="applybutton" >
+ <property name="text" >
+ <string>Apply</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="okbutton" >
+ <property name="text" >
+ <string>Ok</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="cancelbutton" >
+ <property name="text" >
+ <string>Cancel</string>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <layoutdefault spacing="6" margin="11" />
+ <pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
+ <customwidgets>
+ <customwidget>
+ <class>Q3ComboBox</class>
+ <extends>QComboBox</extends>
+ <header>q3combobox.h</header>
+ <container>0</container>
+ <pixmap></pixmap>
+ </customwidget>
+ <customwidget>
+ <class>Q3ButtonGroup</class>
+ <extends></extends>
+ <header>Qt3Support/Q3ButtonGroup</header>
+ <container>1</container>
+ <pixmap></pixmap>
+ </customwidget>
+ <customwidget>
+ <class>Q3GroupBox</class>
+ <extends></extends>
+ <header>Qt3Support/Q3GroupBox</header>
+ <container>1</container>
+ <pixmap></pixmap>
+ </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>
View
68 example/mainwindow.cpp
@@ -15,6 +15,11 @@
#include "mainwindow.h"
+#include "ui_other.h"
+#include "ui_general.h"
+#include <kconfig.h>
+#include <qautoconfigdialog.h>
+
MainWindow::MainWindow()
{
textEdit = new QTextEdit;
@@ -112,6 +117,11 @@ void MainWindow::createActions()
saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
+ settingsAct = new QAction(tr("Settings..."), this);
+ settingsAct->setShortcut(tr("Ctrl+;"));
+ settingsAct->setStatusTip(tr("Configure the application"));
+ connect(settingsAct, SIGNAL(triggered()), this, SLOT(showSettings()));
+
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
@@ -159,6 +169,8 @@ void MainWindow::createMenus()
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addSeparator();
+ fileMenu->addAction(settingsAct);
+ fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
@@ -198,6 +210,17 @@ void MainWindow::readSettings()
QSize size = settings.value("size", QSize(400, 400)).toSize();
resize(size);
move(pos);
+
+ settings.beginGroup("General");
+ helpButton = settings.readBoolEntry("helpbutton", true);
+ cancelButton = settings.readBoolEntry("cancelbutton", true);
+ applyButton = settings.readBoolEntry("applybutton", true);
+ okButton = settings.readBoolEntry("okbutton", true);
+ defaultButton = settings.readBoolEntry("defaultsbutton", true);
+
+ modal = settings.readBoolEntry("modalTrue", false);
+ type = (settings.readNumEntry("type", 1))+1;
+ settings.endGroup();
}
void MainWindow::writeSettings()
@@ -284,3 +307,48 @@ QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
+
+void MainWindow::showSettings()
+{
+ // Check if there is already a dialog and if so bring it to the foreground.
+ if(QAutoConfigDialog::showDialog("settings"))
+ return;
+
+ int dialogButtons = 0;
+ if(helpButton) dialogButtons |= (int)KDialogBase::Help;
+ if(defaultButton) dialogButtons |= (int)KDialogBase::Default;
+ if(okButton) dialogButtons |= (int)KDialogBase::Ok;
+ if(applyButton) dialogButtons |= (int)KDialogBase::Apply;
+ if(cancelButton) dialogButtons |= (int)KDialogBase::Cancel;
+
+ KDialogBase::ButtonCode buttons = (KDialogBase::ButtonCode)dialogButtons;
+ // Create a new dialog with the same name as the above checking code.
+ KConfig *settings = new KConfig("kautoconfigdemo");
+ // BEN FIX memory leak;
+ QAutoConfigDialog *dialog = new QAutoConfigDialog(this, "settings",
+ (KDialogBase::DialogType)type, settings, buttons, modal);
+
+ //dialog->setNamePrefix("kcfg_");
+
+ // Add the general page. Store the settings in the General group and
+ // use the icon package_settings.
+ QWidget *general = new QWidget(0, "General");
+ Ui::General ui;
+ ui.setupUi(general);
+ dialog->addPage(general, "General", "General", "general.png", "General Settings");
+
+
+ if(type == KDialogBase::Tabbed || type == KDialogBase::IconList || type == KDialogBase::Combo){
+ QWidget *other = new QWidget(0, "Other");
+ Ui::Other ui;
+ ui.setupUi(other);
+ dialog->addPage(other, "Other", "Other", "other.png", "Example page of some other settings");
+ }
+
+ // When the user clicks OK or Apply we want to update our settings.
+ connect(dialog, SIGNAL(settingsChanged()), this, SLOT(readSettings()));
+
+ // Display the dialog.
+ dialog->show();
+}
+
View
15 example/mainwindow.h
@@ -37,13 +37,14 @@ private slots:
bool saveAs();
void about();
void documentWasModified();
-
+ void showSettings();
+ void readSettings();
+
private:
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
- void readSettings();
void writeSettings();
bool maybeSave();
void loadFile(const QString &fileName);
@@ -69,6 +70,16 @@ private slots:
QAction *pasteAct;
QAction *aboutAct;
QAction *aboutQtAct;
+ QAction *settingsAct;
+
+ // settings
+ bool helpButton;
+ bool cancelButton;
+ bool applyButton;
+ bool okButton;
+ bool defaultButton;
+ bool modal;
+ int type;
};
#endif
View
104 example/other.ui
@@ -0,0 +1,104 @@
+<ui version="4.0" >
+ <author></author>
+ <comment></comment>
+ <exportmacro></exportmacro>
+ <class>Other</class>
+ <widget class="QWidget" name="Other" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>350</width>
+ <height>308</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Form2</string>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>11</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QLabel" name="textLabel1" >
+ <property name="text" >
+ <string>Uncomment line 339 in application.cpp to enable the saving/retrieving of the below widgets. There names all start with "kcfg_". A prefix can be used as a safty measure to only store widgets that you specify and also makes it easier to build with KDE (which requires the prefix).</string>
+ </property>
+ <property name="alignment" >
+ <set>Qt::AlignTop</set>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="Q3GroupBox" name="kcfg_box" >
+ <property name="title" >
+ <string>kcfg prefixed checkable groupbox</string>
+ </property>
+ <property name="checkable" >
+ <bool>true</bool>
+ </property>
+ <property name="checked" >
+ <bool>true</bool>
+ </property>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>11</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QLineEdit" name="kcfg_lineEdit" >
+ <property name="text" >
+ <string>Example line edit box</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="Q3ComboBox" name="kcfg_comboBox" />
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeType" >
+ <enum>QSizePolicy::Expanding</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>20</width>
+ <height>50</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </widget>
+ <layoutdefault spacing="6" margin="11" />
+ <pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
+ <customwidgets>
+ <customwidget>
+ <class>Q3ComboBox</class>
+ <extends>QComboBox</extends>
+ <header>q3combobox.h</header>
+ <container>0</container>
+ <pixmap></pixmap>
+ </customwidget>
+ <customwidget>
+ <class>Q3GroupBox</class>
+ <extends></extends>
+ <header>Qt3Support/Q3GroupBox</header>
+ <container>1</container>
+ <pixmap></pixmap>
+ </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>
View
2 qautoconfig.pro
@@ -0,0 +1,2 @@
+TEMPLATE = subdirs
+SUBDIRS += example
View
293 src/kautoconfig.h
@@ -1,293 +0,0 @@
-/*
- * Copyright (C) 2003-2005 Benjamin C Meyer (ben at meyerhome dot net)
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
-#ifndef QAUTOCONFIG_H
-#define QAUTOCONFIG_H
-
-#include <qobject.h>
-#include <qlist.h>
-
-class KConfig;
-class QWidget;
-
-/**
- * @short Provides a means of automatically retrieving, saving and resetting
- * basic settings
- *
- * The QAutoConfig class provides a means of automatically retrieving,
- * saving and resetting basic settings. It also can emit signals when
- * settings have been changed (settings were saved) or modified (the
- * user changes a checkbox from on to off).
- *
- * When told to retrieve settings ( retrieveSettings()) QAutoConfig
- * will traverse the specified widgets building a list of all known widgets
- * that have a name and havn't been marked to be ignored.
- * If a setting is marked immutable the value is loaded and the widget is
- * disabled.
- *
- * The name of the widget determines the name of the setting. The initial
- * value of the widget also is the default value when the widget is reset.
- * If the widget does not have a name then it is ignored.
- *
- * When saveSettings() or resetSettings() is called QAutoConfig
- * goes through the list of known widgets and performs the operation on each
- * of them.
- *
- * If one of the widgets needs special treatment it can be specified to be
- * ignored using the ignoreSubWidget() function.
- *
- * QAutoConfig uses the QSqlPropertyMap class to determine if it can do
- * anything to a widget. Note that QAutoConfig doesn't require a database,
- * it simply uses the functionality that is built into the QSqlPropertyMap
- * class. New widgets can be added to the map using
- * QSqlPropertyMap::installDefaultMap(). Note that you can't just add any
- * class. The class must have a matching Q_PROPERTY(...) macro defined.
- *
- * For example (note that KColorButton is already added and it doesn't need to
- * manually added):
- *
- * kcolorbutton.h defines the following property:
- * \code
- * Q_PROPERTY( QColor color READ color WRITE setColor )
- * \endcode
- *
- * To add KColorButton the following code would be inserted in the main.
- *
- * \code
- * QSqlPropertyMap *map = QSqlPropertyMap::defaultMap();
- * map.insert("KColorButton", "color");
- * QSqlPropertyMap::installDefaultMap(map);
- * \endcode
- *
- * If you add a new widget to the QSqlPropertyMap and wish to be notified when
- * it is modified you should add its signal using addWidgetChangedSignal().
- * If the Apply and Default buttons and enabled/disabled by QAutoConfigDialog
- * automatically then this must be done.
- *
- * @see QAutoConfigDialog
- * @author Benjamin C. Meyer <ben+kautoconfig at meyerhome dot net>
- */
-class QAutoConfig : public QObject {
-
-Q_OBJECT
-
-signals:
- /**
- * One or more of the settings have been saved (such as when the user
- * clicks on the Apply button). This is only emitted by saveSettings()
- * whenever one or more setting were changed and consequently saved.
- */
- void settingsChanged();
-
- /**
- * One or more of the settings have been changed.
- * @param widget - The widget group (pass in via addWidget()) that
- * contains the one or more modified setting.
- * @see settingsChanged()
- */
- void settingsChanged( QWidget *widget );
-
- /**
- * If retrieveSettings() was told to track changes then if
- * any known setting was changed this signal will be emitted. Note
- * that a settings can be modified several times and might go back to the
- * original saved state. hasChanged() will tell you if anything has
- * actually changed from the saved values.
- */
- void widgetModified();
-
-
-public:
- /**
- * Constructor.
- * @param kconfig - KConfig to use when retrieving/saving the widgets
- * that QAutoConfig knows about.
- * @param parent - Parent object.
- * @param name - Object name.
- */
- QAutoConfig( KConfig *kconfig, QObject *parent=0, const char *name=0 );
-
-#ifndef QT_ONLY
- /**
- * Constructor.
- * Uses KGlobal::config() when retrieving/saving the widgets that
- * QAutoConfig knows about.
- * @param parent - Parent object.
- * @param name - Object name.
- */
- QAutoConfig( QObject *parent=0, const char *name=0 );
-#endif
-
- /**
- * Destructor. Deletes private class.
- */
- ~QAutoConfig();
-
- /**
- * Adds a widget to the list of widgets that should be parsed for any
- * children that QAutoConfig might know when retrieveSettings() is
- * called. All calls to this function should be made before calling
- * retrieveSettings().
- * @param widget - Pointer to the widget to add.
- * @param group - Name of the group from which all of the settings for this
- * widget will be located. If a child of 'widget' needs to be in a separate
- * group it should be added separately and also ignored.
- * @see ignoreSubWidget()
- */
- void addWidget( QWidget *widget, const QString &group );
-
- /**
- * Ignore the specified child widget when performing an action. Doesn't
- * effect widgets that were added with addWidget() only their children. All
- * calls to this function should be made before calling retrieveSettings().
- * @param widget - Pointer to the widget that should be ignored.
- * Note: Widgets that don't have a name are ignored automatically.
- **/
- void ignoreSubWidget( QWidget *widget );
-
- /**
- * Traverse the specified widgets to see if anything is different then the
- * current settings. retrieveSettings() must be called before this
- * function to build the list of known widgets and default values.
- * @return bool - True if any settings are different then the stored values.
- */
- bool hasChanged() const;
-
- /**
- * Traverse the specified widgets to see if anything is different then the
- * default. retrieveSettings() must be called before this function to
- * build the list of known widgets and default values.
- * @return bool - True if all of the settings are their default values.
- */
- bool isDefault() const;
-
- /**
- * Adds a widget and its signal to the internal list so that when
- * QAutoConfig finds widgetName in retrieveSettings() it will know
- * how to connect its signal that it has changed to QAutoConfig's signal
- * widgetModified(). This function should be called before
- *
- * Example:
- * \code
- * addWidgetChangedSignal( "QCheckbox", SIGNAL(stateChanged(int)) );
- * \endcode
- *
- * This is generally used in conjunction with the addition of a class
- * to QSqlPropertyMap so QAutoConfig can get/set its values.
- *
- * @param widgetName - The class name of the widget (className()).
- * @param signal - The signal (with "SIGNAL()" wrapper) that should be called.
- */
- inline void addWidgetChangedSignal( const QString &widgetName,
- const QCString &signal ){
- changedMap.insert( widgetName, signal );
- }
-
- /**
- * Traverse the specified widgets, retrieving the settings for all known
- * widgets that aren't being ignored and storing the default values.
- * @param trackChanges - If any changes by the widgets should be tracked
- * set true. This causes the emitting the modified() signal when
- * something changes.
- * @return bool - True if any setting was changed from the default.
- */
- bool retrieveSettings( bool trackChanges=false );
-
- /**
- * Specify the prefix that widgets name must have to be used by QAutoConfig.
- * If the prefix is not set all widgets are used if possible.
- * If you are using KDE's KConfigXT system it default to "kcfg_" so this is
- * provided as a means of keeping your code clean.
- * @param prefix the string to set the prefix to
- */
- void setNamePrefix( const QString &prefix );
-
- /**
- * Reterive the prefix that is set.
- * @return the set prefix
- */
- QString namePrefix() const;
-
-public slots:
- /**
- * Traverse the specified widgets, saving the settings for all known
- * widgets that aren't being ignored. retrieveSettings() must be called
- * before this function to build the list of known widgets and default values.
- * @return bool - True if any settings were changed.
- *
- * Example use: User clicks Ok or Apply button in a configure dialog.
- */
- bool saveSettings();
-
- /**
- * Traverse the specified widgets, reseting the widgets to their default
- * values for all known widgets that aren't being ignored.
- * retrieveSettings() must be called before this function to build
- * the list of known widgets and default values.
- *
- * Example use: User clicks Default button in a configure dialog.
- */
- void resetSettings() const;
-
- /**
- * Traverse the specified widgets, reloading the settings for all known
- * widgets that aren't being ignored.
- * retrieveSettings() must be called before this function to build
- * the list of known widgets and default values.
- *
- * Example use: User clicks Reset button in a configure dialog.
- */
- void reloadSettings() const;
-
-protected:
- /**
- * KConfigBase object used to get/save values.
- */
- KConfig *config;
-
- /**
- * Map of the classes and the signals that they emit when changed.
- */
- QMap<QString, QCString> changedMap;
-
- /**
- * Recursive function that finds all known children.
- * Goes through the children of widget and if any are known and not being
- * ignored, stores them in currentGroup. Also checks if the widget
- * should be disabled because it is set immutable.
- * @param widget - Parent of the children to look at.
- * @param currentGroup - Place to store known children of widget.
- * @param trackChanges - If true then tracks any changes to the children of
- * widget that are known.
- * @return bool - If a widget was set to something other then its default.
- * @see retrieveSettings()
- */
- bool parseChildren( const QWidget *widget,
- QList<QWidget*>&currentGroup, bool trackChanges );
-
-private:
- class QAutoConfigPrivate;
- /**
- * QAutoConfig Private class.
- */
- QAutoConfigPrivate *d;
-
-};
-
-#endif // QAUTOCONFIG_H
-
View
16 src/kconfig.cpp
@@ -42,22 +42,6 @@ KConfig::~KConfig(){
QVariant KConfig::readPropertyEntry (const QString &key, const QVariant &defaultValue) const {
return value(key, defaultValue);
- /*
- switch( defaultValue.type() ) {
- case QVariant::Bool:
- return QVariant(readBoolEntry(key, defaultValue.toBool()));
- case QVariant::Double:
- return QVariant(readDoubleEntry(key, defaultValue.toDouble()));
- case QVariant::Int:
- return QVariant(readNumEntry(key, defaultValue.toInt()));
- case QVariant::StringList: {
- bool readOk;
- QVariant readValue(readListEntry(key, &readOk));
- return readOk ? readValue : defaultValue;
- }
- default:
- return QVariant(readEntry(key, defaultValue.toString()));
- }*/
}
void KConfig::writeEntry (const QString &key, const QVariant &value){
View
23 src/qautoconfig.cpp
@@ -211,8 +211,9 @@ bool QAutoConfig::retrieveSettings(bool trackChanges){
bool usingDefaultValues = false;
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *widget = d->widgets.at(i);
- config->setGroup(d->groups[widget]);
+ config->beginGroup(d->groups[widget]);
usingDefaultValues |= parseChildren(widget, d->autoWidgets[widget], trackChanges);
+ config->endGroup();
}
return usingDefaultValues;
}
@@ -224,7 +225,7 @@ bool QAutoConfig::saveSettings() {
// Go through all of the widgets
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *widget = d->widgets.at(i);
- config->setGroup(d->groups[widget]);
+ config->beginGroup(d->groups[widget]);
// Go through the known autowidgets of this widget and save
bool widgetChanged = false;
for (int i = 0; i < d->autoWidgets[widget].size(); ++i) {
@@ -252,6 +253,7 @@ bool QAutoConfig::saveSettings() {
d->changed |= widgetChanged;
if(widgetChanged)
emit( settingsChanged(widget) );
+ config->endGroup();
}
if(d->changed){
@@ -270,7 +272,7 @@ bool QAutoConfig::hasChanged() const {
// Go through all of the widgets
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *widget = d->widgets.at(i);
- config->setGroup(d->groups[widget]);
+ config->beginGroup(d->groups[widget]);
// Go through the known autowidgets of this widget and save
for (int i = 0; i < d->autoWidgets[widget].size(); ++i) {
QWidget *groupWidget = d->autoWidgets[widget].at(i);
@@ -281,9 +283,12 @@ bool QAutoConfig::hasChanged() const {
// Return once just one item is found to have changed.
if((currentValue == defaultValue && savedValue != currentValue) ||
- (savedValue != currentValue))
+ (savedValue != currentValue)){
+ config->endGroup();
return true;
+ }
}
+ config->endGroup();
}
return false;
}
@@ -295,17 +300,19 @@ bool QAutoConfig::isDefault() const {
// Go through all of the widgets
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *widget = d->widgets.at(i);
- config->setGroup(d->groups[widget]);
+ config->beginGroup(d->groups[widget]);
// Go through the known autowidgets of this widget and save
for (int i = 0; i < d->autoWidgets[widget].size(); ++i) {
QWidget *groupWidget = d->autoWidgets[widget].at(i);
QVariant defaultValue = d->defaultValues[groupWidget];
QVariant currentValue = propertyMap->property(groupWidget);
if(currentValue != defaultValue){
//qDebug("groupWidget %s, has changed: default: %s new: %s", groupWidget->name(), defaultValue.toString().latin1(), currentValue.toString().latin1());
+ config->endGroup();
return false;
}
}
+ config->endGroup();
}
return true;
}
@@ -317,7 +324,7 @@ void QAutoConfig::resetSettings() const {
// Go through all of the widgets
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *widget = d->widgets.at(i);
- config->setGroup(d->groups[widget]);
+ config->beginGroup(d->groups[widget]);
// Go through the known autowidgets of this widget and save
for (int i = 0; i < d->autoWidgets[widget].size(); ++i) {
@@ -328,6 +335,7 @@ void QAutoConfig::resetSettings() const {
d->changed = true;
}
}
+ config->endGroup();
}
}
@@ -338,7 +346,7 @@ void QAutoConfig::reloadSettings() const {
// Go through all of the widgets
for (int i = 0; i < d->widgets.size(); ++i) {
QWidget *pageWidget = d->widgets.at(i);
- config->setGroup(d->groups[pageWidget]);
+ config->beginGroup(d->groups[pageWidget]);
// Go through the known widgets of this page and reload
for (int i = 0; i < d->autoWidgets[pageWidget].size(); ++i) {
@@ -348,6 +356,7 @@ void QAutoConfig::reloadSettings() const {
config->readPropertyEntry(widget->name(), defaultSetting);
propertyMap->setProperty(widget, setting);
}
+ config->endGroup();
}
d->changed = false;
}
View
291 src/qautoconfig.h
@@ -0,0 +1,291 @@
+/*
+ * Copyright (C) 2003-2005 Benjamin C Meyer (ben at meyerhome dot net)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef QAUTOCONFIG_H
+#define QAUTOCONFIG_H
+
+#include <qobject.h>
+#include <qlist.h>
+#include <qhash.h>
+
+class KConfig;
+class QWidget;
+
+/**
+ * @short Provides a means of automatically retrieving, saving and resetting
+ * basic settings
+ *
+ * The QAutoConfig class provides a means of automatically retrieving,
+ * saving and resetting basic settings. It also can emit signals when
+ * settings have been changed (settings were saved) or modified (the
+ * user changes a checkbox from on to off).
+ *
+ * When told to retrieve settings ( retrieveSettings()) QAutoConfig
+ * will traverse the specified widgets building a list of all known widgets
+ * that have a name and havn't been marked to be ignored.
+ * If a setting is marked immutable the value is loaded and the widget is
+ * disabled.
+ *
+ * The name of the widget determines the name of the setting. The initial
+ * value of the widget also is the default value when the widget is reset.
+ * If the widget does not have a name then it is ignored.
+ *
+ * When saveSettings() or resetSettings() is called QAutoConfig
+ * goes through the list of known widgets and performs the operation on each
+ * of them.
+ *
+ * If one of the widgets needs special treatment it can be specified to be
+ * ignored using the ignoreSubWidget() function.
+ *
+ * QAutoConfig uses the QSqlPropertyMap class to determine if it can do
+ * anything to a widget. Note that QAutoConfig doesn't require a database,
+ * it simply uses the functionality that is built into the QSqlPropertyMap
+ * class. New widgets can be added to the map using
+ * QSqlPropertyMap::installDefaultMap(). Note that you can't just add any
+ * class. The class must have a matching Q_PROPERTY(...) macro defined.
+ *
+ * For example (note that KColorButton is already added and it doesn't need to
+ * manually added):
+ *
+ * kcolorbutton.h defines the following property:
+ * \code
+ * Q_PROPERTY( QColor color READ color WRITE setColor )
+ * \endcode
+ *
+ * To add KColorButton the following code would be inserted in the main.
+ *
+ * \code
+ * QSqlPropertyMap *map = QSqlPropertyMap::defaultMap();
+ * map.insert("KColorButton", "color");
+ * QSqlPropertyMap::installDefaultMap(map);
+ * \endcode
+ *
+ * If you add a new widget to the QSqlPropertyMap and wish to be notified when
+ * it is modified you should add its signal using addWidgetChangedSignal().
+ * If the Apply and Default buttons and enabled/disabled by QAutoConfigDialog
+ * automatically then this must be done.
+ *
+ * @see QAutoConfigDialog
+ * @author Benjamin C. Meyer <ben+kautoconfig at meyerhome dot net>
+ */
+class QAutoConfig : public QObject {
+
+Q_OBJECT
+
+signals:
+ /**
+ * One or more of the settings have been saved (such as when the user
+ * clicks on the Apply button). This is only emitted by saveSettings()
+ * whenever one or more setting were changed and consequently saved.
+ */
+ void settingsChanged();
+
+ /**
+ * One or more of the settings have been changed.
+ * @param widget - The widget group (pass in via addWidget()) that
+ * contains the one or more modified setting.
+ * @see settingsChanged()
+ */
+ void settingsChanged( QWidget *widget );
+
+ /**
+ * If retrieveSettings() was told to track changes then if
+ * any known setting was changed this signal will be emitted. Note
+ * that a settings can be modified several times and might go back to the
+ * original saved state. hasChanged() will tell you if anything has
+ * actually changed from the saved values.
+ */
+ void widgetModified();
+
+
+public:
+ /**
+ * Constructor.
+ * @param kconfig - KConfig to use when retrieving/saving the widgets
+ * that QAutoConfig knows about.
+ * @param parent - Parent object.
+ */
+ QAutoConfig( KConfig *kconfig, QObject *parent=0 );
+
+#ifndef QT_ONLY
+ /**
+ * Constructor.
+ * Uses KGlobal::config() when retrieving/saving the widgets that
+ * QAutoConfig knows about.
+ * @param parent - Parent object.
+ * @param name - Object name.
+ */
+ QAutoConfig( QObject *parent=0 );
+#endif
+
+ /**
+ * Destructor. Deletes private class.
+ */
+ ~QAutoConfig();
+
+ /**
+ * Adds a widget to the list of widgets that should be parsed for any
+ * children that QAutoConfig might know when retrieveSettings() is
+ * called. All calls to this function should be made before calling
+ * retrieveSettings().
+ * @param widget - Pointer to the widget to add.
+ * @param group - Name of the group from which all of the settings for this
+ * widget will be located. If a child of 'widget' needs to be in a separate
+ * group it should be added separately and also ignored.
+ * @see ignoreSubWidget()
+ */
+ void addWidget( QWidget *widget, const QString &group );
+
+ /**
+ * Ignore the specified child widget when performing an action. Doesn't
+ * effect widgets that were added with addWidget() only their children. All
+ * calls to this function should be made before calling retrieveSettings().
+ * @param widget - Pointer to the widget that should be ignored.
+ * Note: Widgets that don't have a name are ignored automatically.
+ **/
+ void ignoreSubWidget( QWidget *widget );
+
+ /**
+ * Traverse the specified widgets to see if anything is different then the
+ * current settings. retrieveSettings() must be called before this
+ * function to build the list of known widgets and default values.
+ * @return bool - True if any settings are different then the stored values.
+ */
+ bool hasChanged() const;
+
+ /**
+ * Traverse the specified widgets to see if anything is different then the
+ * default. retrieveSettings() must be called before this function to
+ * build the list of known widgets and default values.
+ * @return bool - True if all of the settings are their default values.
+ */
+ bool isDefault() const;
+
+ /**
+ * Adds a widget and its signal to the internal list so that when
+ * QAutoConfig finds widgetName in retrieveSettings() it will know
+ * how to connect its signal that it has changed to QAutoConfig's signal
+ * widgetModified(). This function should be called before
+ *
+ * Example:
+ * \code
+ * addWidgetChangedSignal( "QCheckbox", SIGNAL(stateChanged(int)) );
+ * \endcode
+ *
+ * This is generally used in conjunction with the addition of a class
+ * to QSqlPropertyMap so QAutoConfig can get/set its values.
+ *
+ * @param widgetName - The class name of the widget (className()).
+ * @param signal - The signal (with "SIGNAL()" wrapper) that should be called.
+ */
+ inline void addWidgetChangedSignal( const QString &widgetName, const QString &signal ){
+ changedMap.insert( widgetName, signal );
+ }
+
+ /**
+ * Traverse the specified widgets, retrieving the settings for all known
+ * widgets that aren't being ignored and storing the default values.
+ * @param trackChanges - If any changes by the widgets should be tracked
+ * set true. This causes the emitting the modified() signal when
+ * something changes.
+ * @return bool - True if any setting was changed from the default.
+ */
+ bool retrieveSettings( bool trackChanges=false );
+
+ /**
+ * Specify the prefix that widgets name must have to be used by QAutoConfig.
+ * If the prefix is not set all widgets are used if possible.
+ * If you are using KDE's KConfigXT system it default to "kcfg_" so this is
+ * provided as a means of keeping your code clean.
+ * @param prefix the string to set the prefix to
+ */
+ void setNamePrefix( const QString &prefix );
+
+ /**
+ * Reterive the prefix that is set.
+ * @return the set prefix
+ */
+ QString namePrefix() const;
+
+public slots:
+ /**
+ * Traverse the specified widgets, saving the settings for all known
+ * widgets that aren't being ignored. retrieveSettings() must be called
+ * before this function to build the list of known widgets and default values.
+ * @return bool - True if any settings were changed.
+ *
+ * Example use: User clicks Ok or Apply button in a configure dialog.
+ */
+ bool saveSettings();
+
+ /**
+ * Traverse the specified widgets, reseting the widgets to their default
+ * values for all known widgets that aren't being ignored.
+ * retrieveSettings() must be called before this function to build
+ * the list of known widgets and default values.
+ *
+ * Example use: User clicks Default button in a configure dialog.
+ */
+ void resetSettings() const;
+
+ /**
+ * Traverse the specified widgets, reloading the settings for all known
+ * widgets that aren't being ignored.
+ * retrieveSettings() must be called before this function to build
+ * the list of known widgets and default values.
+ *
+ * Example use: User clicks Reset button in a configure dialog.
+ */
+ void reloadSettings() const;
+
+protected:
+ /**
+ * KConfigBase object used to get/save values.
+ */
+ KConfig *config;
+
+ /**
+ * Hash of the classes and the signals that they emit when changed.
+ */
+ QHash<QString, QString> changedMap;
+
+ /**
+ * Recursive function that finds all known children.
+ * Goes through the children of widget and if any are known and not being
+ * ignored, stores them in currentGroup. Also checks if the widget
+ * should be disabled because it is set immutable.
+ * @param widget - Parent of the children to look at.
+ * @param currentGroup - Place to store known children of widget.
+ * @param trackChanges - If true then tracks any changes to the children of
+ * widget that are known.
+ * @return bool - If a widget was set to something other then its default.
+ * @see retrieveSettings()
+ */
+ bool parseChildren( const QWidget *widget, QList<QWidget*>&currentGroup, bool trackChanges );
+
+private:
+ class QAutoConfigPrivate;
+ /**
+ * QAutoConfig Private class.
+ */
+ QAutoConfigPrivate *d;
+
+};
+
+#endif // QAUTOCONFIG_H
+
View
5 src/qautoconfig.pri
@@ -0,0 +1,5 @@
+INCLUDEPATH += $$PWD
+DEPENDPATH += $$PWD
+SOURCES += $$PWD/qautoconfig.cpp $$PWD/qautoconfigdialog.cpp $$PWD/kdialogbase.cpp $$PWD/kconfig.cpp
+HEADERS += $$PWD/qautoconfig.h $$PWD/qautoconfigdialog.h $$PWD/kdialogbase.h $$PWD/kconfig.h
+

0 comments on commit 827306b

Please sign in to comment.