93 lines
2.6 KiB
C
Raw Permalink Normal View History

/*
2020-06-15 17:42:37 +02:00
Copyright 2006-2020 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROPERTIESEDITORDIALOG_H
#define PROPERTIESEDITORDIALOG_H
#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QAbstractButton>
/**
2020-08-16 11:19:36 +02:00
@brief The PropertiesEditorDialog class
Create a dialog to edit some properties of a thing.
2020-08-18 20:07:55 +02:00
Only create a instance of this class and call exec,
all is done for you in this class.
The first argument (a template) must be a subclass
of QWidget and provide the 3 methods bellow :
2020-08-16 11:19:36 +02:00
QString::title()
void::apply()
void::reset()
2020-08-18 20:07:55 +02:00
You can subclass the interface PropertiesEditorWidget
who provide all this methods.
This dialog take ownership of the editor,
so the editor will be deleted by this dialog
2020-08-16 11:19:36 +02:00
*/
class PropertiesEditorDialog : public QDialog
{
Q_OBJECT
public:
template<typename T>
PropertiesEditorDialog(T editor, QWidget *parent = nullptr) :
QDialog (parent)
{
2020-08-18 20:07:55 +02:00
//Set dialog title
setWindowTitle(editor->title());
2020-08-18 20:07:55 +02:00
// Reparent the editor,
// to be deleted at the same time of this dialog
editor->setParent(this);
2020-08-18 20:07:55 +02:00
//Build the dialog
QVBoxLayout *vlayout = new QVBoxLayout(this);
vlayout->addWidget(editor);
2020-08-18 20:08:32 +02:00
QDialogButtonBox *button_box = new QDialogButtonBox (
QDialogButtonBox::Apply
| QDialogButtonBox::Cancel
| QDialogButtonBox::Reset,
this);
vlayout->addWidget(button_box);
2020-08-18 20:08:32 +02:00
//Setup connection between button box and the editor
connect(button_box,
&QDialogButtonBox::clicked,
[editor, button_box, this]
(QAbstractButton *button)
{
switch(button_box->buttonRole(button))
{
case QDialogButtonBox::RejectRole:
editor->reset();
this->reject();
break;
case QDialogButtonBox::ResetRole:
editor->reset();
break;
case QDialogButtonBox::ApplyRole:
editor->apply();
this->accept();
break;
default:
editor->reset();
this->reject();
}
});
}
};
#endif // PROPERTIESEDITORDIALOG_H