Skip to content

Commit

Permalink
preferences/colorpaletteeditor: Get rid of ColorPaletteEditorTableView
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Mar 16, 2020
1 parent 314d8d9 commit 968af43
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 143 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL
src/preferences/effectsettingsmodel.cpp
src/preferences/colorpaletteeditor.cpp
src/preferences/colorpaletteeditormodel.cpp
src/preferences/colorpaletteeditortableview.cpp
src/preferences/colorpalettesettings.cpp
src/preferences/replaygainsettings.cpp
src/preferences/settingsmanager.cpp
Expand Down
1 change: 0 additions & 1 deletion build/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,6 @@ def sources(self, build):
"src/preferences/upgrade.cpp",
"src/preferences/colorpaletteeditor.cpp",
"src/preferences/colorpaletteeditormodel.cpp",
"src/preferences/colorpaletteeditortableview.cpp",
"src/preferences/colorpalettesettings.cpp",
"src/preferences/dlgpreferencepage.cpp",

Expand Down
88 changes: 75 additions & 13 deletions src/preferences/colorpaletteeditor.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#include "preferences/colorpaletteeditor.h"

#include <QColorDialog>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QMenu>
#include <QStandardItemModel>
#include <QTableView>

#include "preferences/colorpalettesettings.h"
#include "util/color/predefinedcolorpalettes.h"

namespace {
const QColor kDefaultPaletteColor(0, 0, 0);
}

ColorPaletteEditor::ColorPaletteEditor(QWidget* parent)
: QWidget(parent),
m_pPaletteNameComboBox(make_parented<QComboBox>()),
m_pSaveButton(make_parented<QPushButton>("Save")),
m_pDeleteButton(make_parented<QPushButton>("Delete")),
m_pResetButton(make_parented<QPushButton>("Reset")),
m_pTableView(make_parented<ColorPaletteEditorTableView>()) {
m_pTableView(make_parented<QTableView>()),
m_pModel(make_parented<ColorPaletteEditorModel>()) {
m_pPaletteNameComboBox->setEditable(true);

QHBoxLayout* pTopLayout = new QHBoxLayout();
Expand All @@ -30,6 +38,65 @@ ColorPaletteEditor::ColorPaletteEditor(QWidget* parent)
setLayout(pLayout);
setContentsMargins(0, 0, 0, 0);

// Set up model
m_pModel->setColumnCount(2);
m_pModel->setHeaderData(0, Qt::Horizontal, tr("Color"), Qt::DisplayRole);
m_pModel->setHeaderData(1, Qt::Horizontal, tr("Assign to Hotcue"), Qt::DisplayRole);
connect(m_pModel,
&ColorPaletteEditorModel::dirtyChanged,
[this](bool bDirty) {
m_pResetButton->setEnabled(bDirty);
});

connect(m_pModel,
&ColorPaletteEditorModel::emptyChanged,
[this](bool bDirty) {
m_pResetButton->setEnabled(bDirty);
});

// Setup up table view
m_pTableView->setShowGrid(false);
m_pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pTableView->setSelectionMode(QAbstractItemView::SingleSelection);
m_pTableView->setDragDropMode(QAbstractItemView::InternalMove);
m_pTableView->setDragDropOverwriteMode(false);
m_pTableView->setContextMenuPolicy(Qt::CustomContextMenu);
m_pTableView->setModel(m_pModel);

connect(m_pTableView,
&QTableView::doubleClicked,
[this](const QModelIndex& index) {
if (index.isValid() && index.column() == 0) {
QColor color = QColorDialog::getColor();
if (color.isValid()) {
m_pModel->setColor(index.row(), color);
}
}
});

connect(m_pTableView,
&QTableView::customContextMenuRequested,
[this](const QPoint& pos) {
QMenu menu(this);

QAction* pAddAction = menu.addAction("Add");
QAction* pRemoveAction = menu.addAction("Remove");
QAction* pAction = menu.exec(m_pTableView->viewport()->mapToGlobal(pos));
if (pAction == pAddAction) {
m_pModel->appendRow(kDefaultPaletteColor);
} else if (pAction == pRemoveAction) {
QModelIndexList selection = m_pTableView->selectionModel()->selectedRows();

if (selection.count() > 0) {
QModelIndex index = selection.at(0);

//row selected
int row = index.row();
m_pModel->removeRow(row);
}
}
});

connect(m_pPaletteNameComboBox,
&QComboBox::editTextChanged,
[this](const QString& text) {
Expand All @@ -49,18 +116,18 @@ ColorPaletteEditor::ColorPaletteEditor(QWidget* parent)
}

if (bPaletteExists) {
m_pResetButton->setEnabled(m_pTableView->isDirty());
if (!m_pTableView->isDirty()) {
m_pResetButton->setEnabled(m_pModel->isDirty());
if (!m_pModel->isDirty()) {
bool bPaletteFound = false;
foreach (const ColorPalette& palette, mixxx::PredefinedColorPalettes::kPalettes) {
if (text == palette.getName()) {
bPaletteFound = true;
m_pTableView->setColorPalette(palette);
m_pModel->setColorPalette(palette);
break;
}
}
if (!bPaletteFound) {
m_pTableView->setColorPalette(colorPaletteSettings.getColorPalette(text, mixxx::PredefinedColorPalettes::kDefaultHotcueColorPalette));
m_pModel->setColorPalette(colorPaletteSettings.getColorPalette(text, mixxx::PredefinedColorPalettes::kDefaultHotcueColorPalette));
}
}
} else {
Expand All @@ -84,8 +151,8 @@ ColorPaletteEditor::ColorPaletteEditor(QWidget* parent)
[this] {
QString paletteName = m_pPaletteNameComboBox->currentText();
ColorPaletteSettings colorPaletteSettings(m_pConfig);
colorPaletteSettings.setColorPalette(paletteName, m_pTableView->getColorPalette(paletteName));
m_pTableView->setDirty(false);
colorPaletteSettings.setColorPalette(paletteName, m_pModel->getColorPalette(paletteName));
m_pModel->setDirty(false);
reset();
m_pPaletteNameComboBox->setCurrentText(paletteName);
emit paletteChanged(paletteName);
Expand All @@ -104,17 +171,12 @@ ColorPaletteEditor::ColorPaletteEditor(QWidget* parent)
}
}
}
m_pTableView->setDirty(false);
m_pModel->setDirty(false);
reset();
if (bPaletteExists) {
m_pPaletteNameComboBox->setCurrentText(paletteName);
}
});
connect(m_pTableView,
&ColorPaletteEditorTableView::dirtyChanged,
[this](bool bDirty) {
m_pResetButton->setEnabled(bDirty);
});
}

void ColorPaletteEditor::reset() {
Expand Down
6 changes: 4 additions & 2 deletions src/preferences/colorpaletteeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include <QComboBox>
#include <QPushButton>
#include <QTableView>
#include <QWidget>

#include "preferences/colorpaletteeditortableview.h"
#include "preferences/colorpaletteeditormodel.h"
#include "preferences/usersettings.h"
#include "util/parented_ptr.h"

Expand All @@ -28,5 +29,6 @@ class ColorPaletteEditor : public QWidget {
parented_ptr<QPushButton> m_pSaveButton;
parented_ptr<QPushButton> m_pDeleteButton;
parented_ptr<QPushButton> m_pResetButton;
parented_ptr<ColorPaletteEditorTableView> m_pTableView;
parented_ptr<QTableView> m_pTableView;
parented_ptr<ColorPaletteEditorModel> m_pModel;
};
32 changes: 32 additions & 0 deletions src/preferences/colorpaletteeditormodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,43 @@ QIcon toQIcon(const QColor& color) {

} // namespace

ColorPaletteEditorModel::ColorPaletteEditorModel(QObject* parent)
: QStandardItemModel(parent),
m_bEmpty(true),
m_bDirty(false) {
connect(this,
&ColorPaletteEditorModel::rowsRemoved,
[this] {
if (rowCount() == 0) {
m_bEmpty = true;
emit emptyChanged(true);
}
setDirty(true);
});
connect(this,
&ColorPaletteEditorModel::rowsInserted,
[this] {
if (m_bEmpty && rowCount() != 0) {
m_bEmpty = false;
emit emptyChanged(true);
}
setDirty(true);
});
connect(this,
&ColorPaletteEditorModel::rowsMoved,
[this] {
setDirty(true);
});
}

bool ColorPaletteEditorModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) {
// Always move the entire row, and don't allow column "shifting"
Q_UNUSED(column);
return QStandardItemModel::dropMimeData(data, action, row, 0, parent);
}

bool ColorPaletteEditorModel::setData(const QModelIndex& modelIndex, const QVariant& value, int role) {
setDirty(true);
if (modelIndex.isValid() && modelIndex.column() == 1) {
bool ok;
int hotcueIndex = value.toInt(&ok);
Expand Down Expand Up @@ -89,6 +119,8 @@ void ColorPaletteEditorModel::setColorPalette(const ColorPalette& palette) {
int colorIndex = hotcueColorIndicesMap.value(i, kNoHotcueIndex);
appendRow(color, colorIndex);
}

setDirty(false);
}

ColorPalette ColorPaletteEditorModel::getColorPalette(const QString& name) const {
Expand Down
29 changes: 22 additions & 7 deletions src/preferences/colorpaletteeditormodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ class ColorPaletteEditorModel : public QStandardItemModel {
public:
static constexpr int kNoHotcueIndex = -1;

ColorPaletteEditorModel(int rows, int columns, QObject* parent = nullptr)
: QStandardItemModel(rows, columns, parent) {
}
ColorPaletteEditorModel(QObject* parent = nullptr)
: QStandardItemModel(parent) {
}
ColorPaletteEditorModel(QObject* parent = nullptr);

bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;

void setColor(int row, const QColor& color);
void setHotcueIndex(int row, int hotcueIndex);
void appendRow(const QColor& color, int hotcueIndex = kNoHotcueIndex);

void setDirty(bool bDirty) {
if (m_bDirty == bDirty) {
return;
}
m_bDirty = bDirty;
emit dirtyChanged(m_bDirty);
}

bool isDirty() {
return m_bDirty;
}

void setColorPalette(const ColorPalette& palette);
ColorPalette getColorPalette(const QString& name) const;

signals:
void emptyChanged(bool bIsEmpty);
void dirtyChanged(bool bIsDirty);

private:
bool m_bEmpty;
bool m_bDirty;
};
82 changes: 0 additions & 82 deletions src/preferences/colorpaletteeditortableview.cpp

This file was deleted.

Loading

0 comments on commit 968af43

Please sign in to comment.