Skip to content

Commit

Permalink
Improve common editor class II
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Dec 10, 2024
1 parent 3c44b12 commit 291c334
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/editors/editor_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QDataWidgetMapper;
/**
* Common editor behaviour with special support for reversing changes if rejected.
*
* Notable, the class requires each mapper to have a model with a single row.
* Notably, the class requires each mapper to have a model with a single row.
*/
class AbstractEditorDialog : public QDialog {
Q_OBJECT
Expand Down
48 changes: 5 additions & 43 deletions src/editors/name_editor_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@
#include <KLocalizedString>
#include <QCompleter>
#include <QDataWidgetMapper>
#include <QDialog>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>

NamesEditorDialog::NamesEditorDialog(QAbstractProxyModel* model, bool newRow, QWidget* parent) :
QDialog(parent),
model(model),
newRow(newRow),
AbstractEditorDialog(newRow, parent),
form(new Ui::NameEditorForm()) {
form->setupUi(this);

// Connect the buttons.
connect(form->dialogButtons, &QDialogButtonBox::accepted, this, &QDialog::accept); // NOLINT(*-unused-return-value)
connect(form->dialogButtons, &QDialogButtonBox::rejected, this, &QDialog::reject); // NOLINT(*-unused-return-value)

// Set up the name origin combobox.
connectComboBox(model, NamesTableModel::ORIGIN, form->origin);

Expand Down Expand Up @@ -62,12 +53,9 @@ NamesEditorDialog::NamesEditorDialog(QAbstractProxyModel* model, bool newRow, QW
form->noteEdit->enableRichTextMode();
connect(form->noteEditButton, &QPushButton::clicked, this, &NamesEditorDialog::editNoteWithEditor);

// TODO: investigate why this does not work with manual submit.
// Possibly since the model uses auto-submit?
// This means cancel does not work.
this->mapper = new QDataWidgetMapper(this);
auto* mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setModel(this->model);
mapper->setModel(model);
mapper->addMapping(form->titles, NamesTableModel::TITLES);
mapper->addMapping(form->givenNames, NamesTableModel::GIVEN_NAMES);
mapper->addMapping(form->prefix, NamesTableModel::PREFIX);
Expand All @@ -76,33 +64,7 @@ NamesEditorDialog::NamesEditorDialog(QAbstractProxyModel* model, bool newRow, QW
mapper->addMapping(form->noteEdit, NamesTableModel::NOTE);
mapper->setItemDelegate(new CustomSqlRelationalDelegate(this));
mapper->toFirst();
}

void NamesEditorDialog::accept() {
// Attempt to submit the mapper changes.
if (mapper->submit()) {
// We are done.
QDialog::accept();
} else {
// Find the original model.
auto* sqlModel = findSourceModelOfType<QSqlQueryModel>(model);
assert(sqlModel != nullptr);
auto lastError = sqlModel->lastError();
qWarning() << "Error was:" << lastError.text();
qDebug() << "Native error code is " << lastError.nativeErrorCode();
qDebug() << "Last query is " << sqlModel->query().lastQuery();
}
}

void NamesEditorDialog::reject() {
this->model->revert();
if (this->newRow) {
qDebug() << "Removing cancelled addition...";
if (!this->model->removeRow(this->model->rowCount() - 1)) {
qWarning() << "Could not revert name...";
}
}
QDialog::reject();
addMapper(mapper);
}

void NamesEditorDialog::editNoteWithEditor() {
Expand All @@ -114,7 +76,7 @@ void NamesEditorDialog::editNoteWithEditor() {
}

NamesEditorDialog::~NamesEditorDialog() {
delete this->form;
delete form;
}

void NamesEditorDialog::showDialogForNewName(QAbstractProxyModel* model, QWidget* parent) {
Expand Down
12 changes: 3 additions & 9 deletions src/editors/name_editor_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#pragma once

#include "editor_dialog.h"

#include <QDialog>

namespace Ui {
Expand All @@ -17,7 +19,7 @@ class QDataWidgetMapper;
/**
* Editor for names.
*/
class NamesEditorDialog : public QDialog {
class NamesEditorDialog : public AbstractEditorDialog {
Q_OBJECT

public:
Expand All @@ -29,16 +31,8 @@ class NamesEditorDialog : public QDialog {
static void showDialogForExistingName(QAbstractProxyModel* model, QWidget* parent);

public Q_SLOTS:
void accept() override;
void reject() override;

void editNoteWithEditor();

private:
QAbstractProxyModel* model;
QDataWidgetMapper* mapper;

bool newRow;

Ui::NameEditorForm* form;
};
225 changes: 127 additions & 98 deletions src/editors/name_editor_dialog.ui
Original file line number Diff line number Diff line change
@@ -1,128 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>NameEditorForm</class>
<widget class="QWidget" name="NameEditorForm">
<widget class="QDialog" name="NameEditorForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>493</width>
<height>404</height>
<width>550</width>
<height>450</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<property name="verticalSpacing">
<number>6</number>
<layout class="QFormLayout" name="formLayout">
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="titleLabel">
<property name="text">
<string>Titles</string>
</property>
<item row="0" column="0">
<widget class="QLabel" name="titleLabel">
<property name="text">
<string>Titles</string>
</property>
<property name="buddy">
<cstring>titles</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="titles"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="givenNamesLabel">
<property name="text">
<string>Given names</string>
</property>
<property name="buddy">
<cstring>givenNames</cstring>
<property name="buddy">
<cstring>titles</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="titles"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="givenNamesLabel">
<property name="text">
<string>Given names</string>
</property>
<property name="buddy">
<cstring>givenNames</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="givenNames"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="surnameLabel">
<property name="text">
<string>Surname</string>
</property>
<property name="buddy">
<cstring>surname</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<item>
<widget class="QLineEdit" name="prefix">
<property name="placeholderText">
<string>Prefix</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="givenNames"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="surnameLabel">
<property name="text">
<string>Surname</string>
</property>
<property name="buddy">
<cstring>surname</cstring>
</property>
</widget>
<item>
<widget class="QLineEdit" name="surname"/>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<item>
<widget class="QLineEdit" name="prefix">
<property name="placeholderText">
<string>Prefix</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="surname"/>
</item>
</layout>
</layout>
</item>
<item row="3" column="0">
<widget class="QLabel" name="originLabel">
<property name="text">
<string>Origin</string>
</property>
<property name="buddy">
<cstring>origin</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="origin">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="noteLabel">
<property name="text">
<string>Note</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout2_2" stretch="0,0">
<item>
<widget class="KRichTextEdit" name="noteEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="originLabel">
<item alignment="Qt::AlignmentFlag::AlignTop">
<widget class="QPushButton" name="noteEditButton">
<property name="text">
<string>Origin</string>
<string/>
</property>
<property name="buddy">
<cstring>origin</cstring>
<property name="icon">
<iconset theme="edit-entry"/>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="origin">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="noteLabel">
<property name="text">
<string>Note</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout2_2" stretch="0,0">
<item>
<widget class="KRichTextEdit" name="noteEdit"/>
</item>
<item alignment="Qt::AlignmentFlag::AlignTop">
<widget class="QPushButton" name="noteEditButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="edit-entry"/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="dialogButtons">
<property name="standardButtons">
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="dialogButtons">
<property name="standardButtons">
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
Expand All @@ -133,5 +129,38 @@
</customwidget>
</customwidgets>
<resources/>
<connections/>
<connections>
<connection>
<sender>dialogButtons</sender>
<signal>accepted()</signal>
<receiver>NameEditorForm</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>274</x>
<y>426</y>
</hint>
<hint type="destinationlabel">
<x>274</x>
<y>224</y>
</hint>
</hints>
</connection>
<connection>
<sender>dialogButtons</sender>
<signal>rejected()</signal>
<receiver>NameEditorForm</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>274</x>
<y>426</y>
</hint>
<hint type="destinationlabel">
<x>274</x>
<y>224</y>
</hint>
</hints>
</connection>
</connections>
</ui>
4 changes: 4 additions & 0 deletions src/editors/new_person_editor_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ NewPersonEditorDialog::NewPersonEditorDialog(QWidget* parent) :
auto* sexesModel = DataManager::get().sexesModel(this);
form->sexComboBox->setModel(sexesModel);
}

NewPersonEditorDialog::~NewPersonEditorDialog() {
delete form;
}
Loading

0 comments on commit 291c334

Please sign in to comment.