Adjust the layout of the Edit Cell as per #673 (#679)

This commit is contained in:
Justin Clift
2016-07-27 20:41:01 +01:00
committed by GitHub
parent 7eb733b31f
commit 89baf3464d
4 changed files with 316 additions and 167 deletions

View File

@@ -7,14 +7,17 @@
#include <QKeySequence>
#include <QShortcut>
#include <QImageReader>
#include <QBuffer>
EditDialog::EditDialog(QWidget* parent)
: QDialog(parent),
ui(new Ui::EditDialog)
{
ui->setupUi(this);
ui->buttonBox->button(QDialogButtonBox::Ok)->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setVisible(true);
// Add Ctrl-Enter (Cmd-Enter on OSX) as a shortcut for the Apply button
ui->buttonApply->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
QHBoxLayout* hexLayout = new QHBoxLayout(ui->editorBinary);
hexEdit = new QHexEdit(this);
@@ -48,7 +51,13 @@ void EditDialog::reset()
ui->editorImage->clear();
hexEdit->setData(QByteArray());
oldData = "";
checkDataType();
// Hide the warning about editing non-text data in text mode
ui->labelBinaryWarning->setVisible(false);
// Update the cell data info in the bottom left of the Edit Cell
int dataType = checkDataType();
updateCellInfo(dataType);
}
void EditDialog::closeEvent(QCloseEvent*)
@@ -69,11 +78,9 @@ void EditDialog::showEvent(QShowEvent*)
void EditDialog::reject()
{
// This is called when pressing the cancel button or hitting the escape key
// Reset all fields and move the cursor back to the table view
loadText(oldData, curRow, curCol);
emit goingAway();
// We override this, to ensure the Escape key doesn't make the Edit Cell
// dock go away
return;
}
void EditDialog::loadText(const QByteArray& data, int row, int col)
@@ -87,11 +94,41 @@ void EditDialog::loadText(const QByteArray& data, int row, int col)
ui->editorText->setPlainText(textData);
hexEdit->setData(data);
// Ensure the newly loaded data is all selected in the Edit Cell dock/win
ui->editorText->selectAll();
// Determine the data type in the cell
int dataType = checkDataType();
// Update the cell data information
checkDataType();
// Do some data type specific handling
QImage img;
switch (dataType) {
case Text:
// For text, ensure it's all selected in the Edit Cell
ui->editorText->selectAll();
// Clear any image from the image viewing widget
ui->editorImage->setPixmap(QPixmap(0,0));
break;
case Image:
// For images, load the image into the image viewing widget
if (img.loadFromData(hexEdit->data())) {
ui->editorImage->setPixmap(QPixmap::fromImage(img));
}
break;
default:
// Clear the image viewing widget for everything else too
ui->editorImage->setPixmap(QPixmap(0,0));
break;
}
// Get the current editor mode (eg text, hex, or image mode)
int editMode = ui->editorStack->currentIndex();
// Show or hide the warning about editing binary data in text mode
updateBinaryEditWarning(editMode, dataType);
// Update the cell data info in the bottom left of the Edit Cell
updateCellInfo(dataType);
}
void EditDialog::importData()
@@ -117,8 +154,11 @@ void EditDialog::importData()
QByteArray d = file.readAll();
hexEdit->setData(d);
ui->editorText->setPlainText(d);
checkDataType();
file.close();
// Update the cell data info in the bottom left of the Edit Cell
int dataType = checkDataType();
updateCellInfo(dataType);
}
}
}
@@ -141,12 +181,16 @@ void EditDialog::exportData()
}
}
void EditDialog::clearData()
void EditDialog::setNull()
{
ui->editorText->clear();
ui->editorImage->clear();
hexEdit->setData(QByteArray());
checkDataType();
// Update the cell data info in the bottom left of the Edit Cell
int dataType = checkDataType();
updateCellInfo(dataType);
ui->editorText->setFocus();
}
@@ -156,12 +200,23 @@ void EditDialog::accept()
// To differentiate NULL and empty byte arrays, we also compare the NULL flag
if(hexEdit->data() != oldData || hexEdit->data().isNull() != oldData.isNull())
{
const QString dataType = ui->comboEditor->currentText();
bool isBlob = dataType == tr("Binary") || !ui->comboEditor->isVisible();
const QString dataType = ui->comboMode->currentText();
bool isBlob = dataType == tr("Binary");
emit updateRecordText(curRow, curCol, isBlob, hexEdit->data());
}
}
// Called when the user manually changes the "Mode" drop down combobox
void EditDialog::editModeChanged(int editMode)
{
// Switch to the selected editor
ui->editorStack->setCurrentIndex(editMode);
// Show or hide the warning about editing binary data in text mode
int dataType = checkDataType();
updateBinaryEditWarning(editMode, dataType);
}
void EditDialog::editTextChanged()
{
if(ui->editorText->hasFocus())
@@ -169,7 +224,10 @@ void EditDialog::editTextChanged()
hexEdit->blockSignals(true);
hexEdit->setData(ui->editorText->toPlainText().toUtf8());
hexEdit->blockSignals(false);
checkDataType();
// Update the cell data info in the bottom left of the Edit Cell
int dataType = checkDataType();
updateCellInfo(dataType);
}
}
@@ -181,46 +239,31 @@ void EditDialog::hexDataChanged()
ui->editorText->blockSignals(false);
}
void EditDialog::checkDataType()
// Determine the type of data in the cell
int EditDialog::checkDataType()
{
ui->comboEditor->setVisible(true);
QByteArray cellData = hexEdit->data();
// Assume NULL type first
if (hexEdit->data().isNull())
ui->labelType->setText(tr("Type of data currently in cell: Null"));
// Check if data is text only
if(QString(hexEdit->data()).toUtf8() == hexEdit->data()) // Any proper way??
{
ui->labelBinaryWarning->setVisible(false);
if (!hexEdit->data().isNull())
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", hexEdit->data().length()));
} else {
// It's not. So it might be an image.
QImage img;
if(img.loadFromData(hexEdit->data()))
{
// It is.
ui->editorImage->setPixmap(QPixmap::fromImage(img));
ui->labelType->setText(tr("Type of data currently in cell: Image"));
ui->labelSize->setText(tr("%1x%2 pixel").arg(ui->editorImage->pixmap()->size().width()).arg(ui->editorImage->pixmap()->size().height()));
ui->comboEditor->setVisible(false);
} else {
// It's not. So it's probably some random binary data.
ui->labelBinaryWarning->setVisible(true);
if (!hexEdit->data().isNull())
ui->labelType->setText(tr("Type of data currently in cell: Binary"));
ui->labelSize->setText(tr("%n byte(s)", "", hexEdit->data().length()));
}
// Check for NULL data type
if (cellData.isNull()) {
return Null;
}
// Check if it's an image
QBuffer imageBuffer(&cellData);
QImageReader readerBuffer(&imageBuffer);
bool someCheck = readerBuffer.canRead();
if (someCheck == true) {
return Image;
}
// Check if it's text only
if (QString(cellData).toUtf8() == cellData) { // Is there a better way to check this?
return Text;
}
// It's none of the above, so treat it as general binary data
return Binary;
}
void EditDialog::toggleOverwriteMode()
@@ -245,10 +288,69 @@ void EditDialog::setFocus()
ui->editorText->selectAll();
}
// Enables or disables the OK/Clear/Import buttons in the Edit Cell dock
// Enables or disables the Apply, Null, & Import buttons in the Edit Cell dock
void EditDialog::allowEditing(bool on)
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(on);
ui->buttonClear->setEnabled(on);
ui->buttonApply->setEnabled(on);
ui->buttonNull->setEnabled(on);
ui->buttonImport->setEnabled(on);
}
// Shows or hides the warning about editing binary data in text mode
void EditDialog::updateBinaryEditWarning(int editMode, int dataType)
{
// If we're in the text editor, and the data type is not plain text
// display a warning about editing it
if ((editMode == 0) && ((dataType != Text) && (dataType != Null))) {
// Display the warning
ui->labelBinaryWarning->setVisible(true);
} else {
// Hide the warning
ui->labelBinaryWarning->setVisible(false);
}
}
// Update the information labels in the bottom left corner of the dialog
void EditDialog::updateCellInfo(int cellType)
{
QByteArray cellData = hexEdit->data();
// Image data needs special treatment
if (cellType == Image) {
QBuffer imageBuffer(&cellData);
QImageReader image(&imageBuffer);
// Display the image format
QString imageFormat = image.format();
ui->labelType->setText(tr("Type of data currently in cell: %1 Image").arg(imageFormat.toUpper()));
// Display the image dimensions
QSize imageSize = image.size();
ui->labelSize->setText(tr("%1x%2 pixel").arg(imageSize.width()).arg(imageSize.height()));
return;
}
// Determine the legth of the cell data
int dataLength = cellData.length();
// Use a switch statement for the other data types to keep things neat :)
switch (cellType) {
case Null:
// NULL data type
ui->labelType->setText(tr("Type of data currently in cell: NULL"));
ui->labelSize->setText(tr("%n byte(s)", "", dataLength));
break;
case Text:
// Text only
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", dataLength));
break;
default:
// If none of the above data types, consider it general binary data
ui->labelType->setText(tr("Type of data currently in cell: Binary"));
ui->labelSize->setText(tr("%n byte(s)", "", dataLength));
break;
}
}

View File

@@ -34,12 +34,15 @@ protected:
private slots:
virtual void importData();
virtual void exportData();
virtual void clearData();
virtual void setNull();
virtual void accept();
virtual void editTextChanged();
virtual void hexDataChanged();
virtual void checkDataType();
virtual int checkDataType();
virtual void toggleOverwriteMode();
virtual void editModeChanged(int editMode);
virtual void updateBinaryEditWarning(int editMode, int dataType);
virtual void updateCellInfo(int cellType);
signals:
void goingAway();
@@ -51,6 +54,13 @@ private:
QByteArray oldData;
int curCol;
int curRow;
enum DataType {
Binary,
Image,
Null,
Text
};
};
#endif

View File

@@ -17,29 +17,41 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="buttonImport">
<property name="toolTip">
<string>Import text</string>
</property>
<property name="whatsThis">
<string>Opens a file dialog used to import text to this database cell.</string>
<widget class="QLabel" name="labelMode">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>&amp;Import</string>
<string>Mode:</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonExport">
<property name="toolTip">
<string>Export text</string>
</property>
<property name="whatsThis">
<string>Opens a file dialog used to export the contents of this database cell to a text file.</string>
</property>
<property name="text">
<string>&amp;Export</string>
<widget class="QComboBox" name="comboMode">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<item>
<property name="text">
<string>Text</string>
</property>
</item>
<item>
<property name="text">
<string>Binary</string>
</property>
</item>
<item>
<property name="text">
<string>Image</string>
</property>
</item>
</widget>
</item>
<item>
@@ -56,29 +68,59 @@
</spacer>
</item>
<item>
<widget class="QComboBox" name="comboEditor">
<item>
<property name="text">
<string>Text</string>
</property>
</item>
<item>
<property name="text">
<string>Binary</string>
</property>
</item>
<widget class="QPushButton" name="buttonImport">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Import text</string>
</property>
<property name="whatsThis">
<string>Opens a file dialog used to import text to this database cell.</string>
</property>
<property name="text">
<string>&amp;Import</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonClear">
<widget class="QPushButton" name="buttonExport">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Clear cell data</string>
<string>Export text</string>
</property>
<property name="whatsThis">
<string>Opens a file dialog used to export the contents of this database cell to a text file.</string>
</property>
<property name="text">
<string>&amp;Export</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonNull">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Set this cell to NULL</string>
</property>
<property name="whatsThis">
<string>Erases the contents of the cell</string>
</property>
<property name="text">
<string>&amp;Clear</string>
<string>Set as &amp;NULL</string>
</property>
</widget>
</item>
@@ -125,7 +167,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<width>83</width>
<height>40</height>
</rect>
</property>
@@ -163,12 +205,25 @@
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonApply">
<property name="text">
<string>Apply</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
@@ -177,46 +232,11 @@
</layout>
</widget>
<tabstops>
<tabstop>buttonImport</tabstop>
<tabstop>buttonExport</tabstop>
<tabstop>comboEditor</tabstop>
<tabstop>buttonClear</tabstop>
<tabstop>buttonBox</tabstop>
<tabstop>buttonNull</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>EditDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>262</x>
<y>370</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>EditDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>326</x>
<y>350</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonImport</sender>
<signal>clicked()</signal>
@@ -224,8 +244,8 @@
<slot>importData()</slot>
<hints>
<hint type="sourcelabel">
<x>41</x>
<y>16</y>
<x>400</x>
<y>28</y>
</hint>
<hint type="destinationlabel">
<x>60</x>
@@ -240,8 +260,8 @@
<slot>exportData()</slot>
<hints>
<hint type="sourcelabel">
<x>100</x>
<y>20</y>
<x>488</x>
<y>51</y>
</hint>
<hint type="destinationlabel">
<x>162</x>
@@ -249,22 +269,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonClear</sender>
<signal>clicked()</signal>
<receiver>EditDialog</receiver>
<slot>clearData()</slot>
<hints>
<hint type="sourcelabel">
<x>321</x>
<y>13</y>
</hint>
<hint type="destinationlabel">
<x>290</x>
<y>12</y>
</hint>
</hints>
</connection>
<connection>
<sender>editorText</sender>
<signal>textChanged()</signal>
@@ -282,34 +286,66 @@
</hints>
</connection>
<connection>
<sender>comboEditor</sender>
<signal>currentIndexChanged(int)</signal>
<receiver>editorStack</receiver>
<sender>editorStack</sender>
<signal>currentChanged(int)</signal>
<receiver>comboMode</receiver>
<slot>setCurrentIndex(int)</slot>
<hints>
<hint type="sourcelabel">
<x>265</x>
<y>16</y>
</hint>
<hint type="destinationlabel">
<x>185</x>
<y>169</y>
</hint>
<hint type="destinationlabel">
<x>149</x>
<y>39</y>
</hint>
</hints>
</connection>
<connection>
<sender>editorStack</sender>
<signal>currentChanged(int)</signal>
<receiver>comboEditor</receiver>
<slot>setCurrentIndex(int)</slot>
<sender>buttonApply</sender>
<signal>clicked()</signal>
<receiver>EditDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>169</y>
<x>605</x>
<y>358</y>
</hint>
<hint type="destinationlabel">
<x>267</x>
<y>16</y>
<x>241</x>
<y>406</y>
</hint>
</hints>
</connection>
<connection>
<sender>comboMode</sender>
<signal>currentIndexChanged(int)</signal>
<receiver>EditDialog</receiver>
<slot>editModeChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>71</x>
<y>27</y>
</hint>
<hint type="destinationlabel">
<x>201</x>
<y>431</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonNull</sender>
<signal>clicked()</signal>
<receiver>EditDialog</receiver>
<slot>setNull()</slot>
<hints>
<hint type="sourcelabel">
<x>528</x>
<y>28</y>
</hint>
<hint type="destinationlabel">
<x>723</x>
<y>58</y>
</hint>
</hints>
</connection>
@@ -317,8 +353,9 @@
<slots>
<slot>importData()</slot>
<slot>exportData()</slot>
<slot>clearData()</slot>
<slot>editTextChanged()</slot>
<slot>checkDataType()</slot>
<slot>editModeChanged(int)</slot>
<slot>setNull()</slot>
</slots>
</ui>

View File

@@ -831,7 +831,7 @@ void MainWindow::doubleClickTable(const QModelIndex& index)
bool allowEditing = (m_currentTabTableModel == m_browseTableModel) &&
(db.getObjectByName(ui->comboBrowseTable->currentText()).gettype() == "table");
// Enable or disable the OK, Clear, & Import buttons in the Edit Cell dock
// Enable or disable the Apply & Import buttons in the Edit Cell dock
// depending on the value of the "allowEditing" bool above
editDock->allowEditing(allowEditing);