Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Disable completely editing, including layout changes, when read only …
Browse files Browse the repository at this point in the history
…sync session is active, fixes #36
  • Loading branch information
joshirio committed Oct 13, 2017
1 parent b9e2f59 commit 78bc8c8
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 22 deletions.
17 changes: 17 additions & 0 deletions models/standardmodel.cpp
Expand Up @@ -11,6 +11,7 @@
#include "../components/metadataengine.h"
#include "../utils/metadatapropertiesparser.h"
#include "../components/alarmmanager.h"
#include "../components/sync_framework/syncsession.h"

#include <QtSql/QSqlRecord>

Expand Down Expand Up @@ -170,3 +171,19 @@ int StandardModel::realRowCount()

return rowCount();
}

bool StandardModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
//avoid data changes on read-only mode
if (!SyncSession::IS_READ_ONLY) {
//set local data changed
SyncSession::LOCAL_DATA_CHANGED = true; //already set elsewhere (redundant)
//TODO: cleanup and decide if better to handle data change sets here
//or like it is now done redundantly on tableview and formview?

return QSqlTableModel::setData(index, value, role);
} else {
return false;
}
}
3 changes: 3 additions & 0 deletions models/standardmodel.h
Expand Up @@ -66,6 +66,9 @@ class StandardModel : public QSqlTableModel
*/
int realRowCount();

/** Reimplement to avoid edits on read only session */
bool setData(const QModelIndex &index, const QVariant &value, int role);

signals:
/** Emitted after a model sort operation */
void modelSortedSignal(int column);
Expand Down
10 changes: 10 additions & 0 deletions views/formview/formview.cpp
Expand Up @@ -334,6 +334,9 @@ void FormView::rowsInserted(const QModelIndex &parent, int start, int end)

void FormView::contextMenuEvent(QContextMenuEvent *event)
{
if (SyncSession::IS_READ_ONLY)
return;

QMenu menu(this);
menu.addAction(m_newRecordContextAction);
menu.addAction(m_duplicateRecordContextAction);
Expand Down Expand Up @@ -466,6 +469,9 @@ void FormView::resizeEvent(QResizeEvent *event)

void FormView::mousePressEvent(QMouseEvent *event)
{
if (SyncSession::IS_READ_ONLY)
return;

//set start drag pos for a possible drag operation
//the position is recorded so that in mouseMoveEvent
//it is possible to distinguish mouse clicks from
Expand Down Expand Up @@ -517,6 +523,10 @@ void FormView::mousePressEvent(QMouseEvent *event)
}
void FormView::mouseMoveEvent(QMouseEvent *event)
{
if(SyncSession::IS_READ_ONLY) {
return QAbstractItemView::mouseMoveEvent(event);
}

//TODO: do some optimizations/clean up for code reuse in this method
bool startFWDrag = false;

Expand Down
18 changes: 18 additions & 0 deletions views/tableview/editors/filestypeeditor.cpp
Expand Up @@ -9,6 +9,7 @@
#include "filestypeeditor.h"
#include "../../../components/filemanager.h"
#include "../../../components/metadataengine.h"
#include "../../../components/sync_framework/syncsession.h"

#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
Expand All @@ -19,6 +20,7 @@
#include <QtWidgets/QProgressDialog>
#include <QtWidgets/QFrame>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMessageBox>


//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -81,6 +83,22 @@ QString FilesTypeEditor::getFiles()

void FilesTypeEditor::browseButtonClicked()
{
if (SyncSession::IS_READ_ONLY) {
//on invalid input show message
QString errorMessage;
errorMessage.append(QObject::tr("Read-only mode: "
"Editing is not allowed."));
QWidget *parent = qobject_cast<QWidget*>(this->parent());
QMessageBox box(QMessageBox::Warning, tr("Invalid Input"),
tr("The entered data is not valid!<br>"
"%1").arg(errorMessage),
QMessageBox::NoButton,
parent);
box.setWindowModality(Qt::WindowModal);
box.exec();
return;
}

QStringList fileList = QFileDialog::getOpenFileNames(this,
tr("Import Files"),
QDir::homePath()
Expand Down
18 changes: 18 additions & 0 deletions views/tableview/editors/imagetypeeditor.cpp
Expand Up @@ -9,6 +9,7 @@
#include "imagetypeeditor.h"
#include "../../../components/filemanager.h"
#include "../../../components/metadataengine.h"
#include "../../../components/sync_framework/syncsession.h"

#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
Expand All @@ -17,6 +18,7 @@
#include <QtCore/QFile>
#include <QtWidgets/QProgressDialog>
#include <QtWidgets/QFrame>
#include <QtWidgets/QMessageBox>


//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -76,6 +78,22 @@ int ImageTypeEditor::getImage()

void ImageTypeEditor::browseButtonClicked()
{
if (SyncSession::IS_READ_ONLY) {
//on invalid input show message
QString errorMessage;
errorMessage.append(QObject::tr("Read-only mode: "
"Editing is not allowed."));
QWidget *parent = qobject_cast<QWidget*>(this->parent());
QMessageBox box(QMessageBox::Warning, tr("Invalid Input"),
tr("The entered data is not valid!<br>"
"%1").arg(errorMessage),
QMessageBox::NoButton,
parent);
box.setWindowModality(Qt::WindowModal);
box.exec();
return;
}

QString file = QFileDialog::getOpenFileName(this,
tr("Import Image"),
QDir::homePath(),
Expand Down
21 changes: 19 additions & 2 deletions widgets/form_widgets/checkboxformwidget.cpp
Expand Up @@ -84,8 +84,25 @@ void CheckboxFormWidget::loadMetadataDisplayProperties(const QString &metadata)

void CheckboxFormWidget::validateData()
{
//always valid
emit dataEdited();
bool valid;

QString editMetadata = MetadataEngine::getInstance().getFieldProperties(
MetadataEngine::EditProperty, getFieldId());
FormWidgetValidator validator(editMetadata, MetadataEngine::CheckboxType);
QString errorMessage;

valid = validator.validate(getData(), errorMessage);

if (valid) {
emit dataEdited();
} else {
//restore last valid value
m_checkbox->setChecked(!m_checkbox->isChecked());

//inform FormView that the widget needs attention
//by animating the widget
emit requiresAttention(errorMessage);
}
}


Expand Down
23 changes: 21 additions & 2 deletions widgets/form_widgets/comboboxformwidget.cpp
Expand Up @@ -67,6 +67,7 @@ void ComboboxFormWidget::setData(const QVariant &data)
value = data.toInt(&validData);
if (validData) {
m_comboBox->setCurrentIndex(value);
m_lastValidIndex = value;
} else {
m_comboBox->setCurrentIndex(m_default);
}
Expand Down Expand Up @@ -114,8 +115,26 @@ void ComboboxFormWidget::loadMetadataDisplayProperties(const QString &metadata)

void ComboboxFormWidget::validateData()
{
//always valid
emit dataEdited();
bool valid;

QString editMetadata = MetadataEngine::getInstance().getFieldProperties(
MetadataEngine::EditProperty, getFieldId());
FormWidgetValidator validator(editMetadata, MetadataEngine::ComboboxType);
QString errorMessage;

valid = validator.validate(getData(), errorMessage);

if (valid) {
m_lastValidIndex = m_comboBox->currentIndex();
emit dataEdited();
} else {
//restore last valid value
m_comboBox->setCurrentIndex(m_lastValidIndex);

//inform FormView that the widget needs attention
//by animating the widget
emit requiresAttention(errorMessage);
}
}


Expand Down
1 change: 1 addition & 0 deletions widgets/form_widgets/comboboxformwidget.h
Expand Up @@ -70,6 +70,7 @@ protected slots:
QStringList m_itemNameList;
int m_default; /**< The default value */
bool m_markEmpty; /**< If data is empty mark field */
int m_lastValidIndex; /**< Last valid index */
};

#endif // COMBOBOXFORMWIDGET_H
36 changes: 32 additions & 4 deletions widgets/form_widgets/dateformwidget.cpp
Expand Up @@ -9,6 +9,7 @@
#include "dateformwidget.h"
#include "../../utils/platformcolorservice.h"
#include "../../utils/metadatapropertiesparser.h"
#include "../../utils/formwidgetvalidator.h"
#include "../../components/metadataengine.h"
#include "../../views/formview/formview.h"
#include "../../components/alarmmanager.h"
Expand All @@ -31,6 +32,7 @@ DateFormWidget::DateFormWidget(QWidget *parent) :
m_fieldNameLabel = new QLabel("Invalid Name", this);
m_dateTimeEdit = new QDateTimeEdit(this);
m_mainLayout = new QVBoxLayout(this);
m_lastValidDateTime = new QDateTime();

//static styling
m_fieldNameLabel->setStyleSheet("QLabel {color: gray;}");
Expand Down Expand Up @@ -67,6 +69,11 @@ DateFormWidget::DateFormWidget(QWidget *parent) :
setupFocusPolicy();
}

DateFormWidget::~DateFormWidget()
{
delete m_lastValidDateTime;
}

void DateFormWidget::setFieldName(const QString &name)
{
m_fieldNameLabel->setText(name);
Expand All @@ -80,14 +87,17 @@ QString DateFormWidget::getFieldName() const
void DateFormWidget::clearData()
{
m_dateTimeEdit->setDateTime(QDateTime(QDate(2000, 01, 01), QTime(00, 00)));
*m_lastValidDateTime = (QDateTime(QDate(2000, 01, 01), QTime(00, 00)));
}

void DateFormWidget::setData(const QVariant &data)
{
if (!data.isNull())
if (!data.isNull()) {
m_dateTimeEdit->setDateTime(data.toDateTime());
else
*m_lastValidDateTime = data.toDateTime();
} else {
clearData();
}
}

QVariant DateFormWidget::getData() const
Expand Down Expand Up @@ -130,8 +140,26 @@ void DateFormWidget::loadMetadataDisplayProperties(const QString &metadata)

void DateFormWidget::validateData()
{
//always valid
emit dataEdited();
bool valid;

QString editMetadata = MetadataEngine::getInstance().getFieldProperties(
MetadataEngine::EditProperty, getFieldId());
FormWidgetValidator validator(editMetadata, MetadataEngine::DateType);
QString errorMessage;

valid = validator.validate(getData(), errorMessage);

if (valid) {
*m_lastValidDateTime = m_dateTimeEdit->dateTime();
emit dataEdited();
} else {
//restore last valid value
m_dateTimeEdit->setDateTime(*m_lastValidDateTime);

//inform FormView that the widget needs attention
//by animating the widget
emit requiresAttention(errorMessage);
}
}

void DateFormWidget::editingFinishedSlot()
Expand Down
3 changes: 3 additions & 0 deletions widgets/form_widgets/dateformwidget.h
Expand Up @@ -23,6 +23,7 @@
class QLabel;
class QDateTimeEdit;
class QVBoxLayout;
class QDateTime;


//-----------------------------------------------------------------------------
Expand All @@ -35,6 +36,7 @@ class DateFormWidget : public AbstractFormWidget

public:
DateFormWidget(QWidget *parent = 0);
~DateFormWidget();

void setFieldName(const QString &name);
QString getFieldName() const;
Expand Down Expand Up @@ -65,6 +67,7 @@ protected slots:
QLabel *m_fieldNameLabel;
QDateTimeEdit *m_dateTimeEdit;
QVBoxLayout *m_mainLayout;
QDateTime *m_lastValidDateTime;
};

#endif // DATEFORMWIDGET_H

0 comments on commit 78bc8c8

Please sign in to comment.