diff --git a/libs/libkis/PaletteView.cpp b/libs/libkis/PaletteView.cpp index fa4319078a..8b4408c1d2 100644 --- a/libs/libkis/PaletteView.cpp +++ b/libs/libkis/PaletteView.cpp @@ -1,84 +1,86 @@ /* * Copyright (c) 2017 Wolthera van Hövell tot Westerflier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include struct PaletteView::Private { KisPaletteModel *model = 0; KisPaletteView *widget = 0; bool allowPaletteModification = true; }; PaletteView::PaletteView(QWidget *parent) : QWidget(parent), d(new Private) { d->widget = new KisPaletteView(this); d->model = new KisPaletteModel(); d->widget->setPaletteModel(d->model); this->setLayout(new QVBoxLayout()); this->layout()->addWidget(d->widget); //forward signals. connect(d->widget, SIGNAL(entrySelected(KisSwatch)), this, SIGNAL(entrySelectedForeGround(KisSwatch))); connect(d->widget, SIGNAL(entrySelectedBackGround(KisSwatch)), this, SIGNAL(entrySelectedBackGround(KisSwatch))); } PaletteView::~PaletteView() { delete d->model; } void PaletteView::setPalette(Palette *palette) { d->model->setColorSet(palette->colorSet()); d->widget->setPaletteModel(d->model); } +/* bool PaletteView::addEntryWithDialog(ManagedColor *color) { if (d->model->colorSet()) { return d->widget->addEntryWithDialog(color->color()); } return false; } bool PaletteView::addGroupWithDialog() { if (d->model->colorSet()) { // d->widget->addGroupWithDialog(); return false; } return false; } bool PaletteView::removeSelectedEntryWithDialog() { if (d->model->colorSet()) { return d->widget->removeEntryWithDialog(d->widget->currentIndex()); } return false; } +*/ void PaletteView::trySelectClosestColor(ManagedColor *color) { d->widget->selectClosestColor(color->color()); } diff --git a/libs/libkis/PaletteView.h b/libs/libkis/PaletteView.h index 3f3909befd..8f004b652e 100644 --- a/libs/libkis/PaletteView.h +++ b/libs/libkis/PaletteView.h @@ -1,98 +1,98 @@ /* * Copyright (c) 2017 Wolthera van Hövell tot Westerflier * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef LIBKIS_PALETTE_VIEW_H #define LIBKIS_PALETTE_VIEW_H #include #include #include "kritalibkis_export.h" #include "libkis.h" #include "Palette.h" #include "ManagedColor.h" #include "KoColorSet.h" #include #include /** * @brief The PaletteView class is a wrapper around a MVC method for handling * palettes. This class shows a nice widget that can drag and drop, edit colors in a colorset * and will handle adding and removing entries if you'd like it to. */ class KRITALIBKIS_EXPORT PaletteView : public QWidget { Q_OBJECT public: PaletteView(QWidget *parent = 0); ~PaletteView(); public Q_SLOTS: /** * @brief setPalette * Set a new palette. * @param palette */ void setPalette(Palette *palette); /** * @brief addEntryWithDialog * This gives a simple dialog for adding colors, with options like * adding name, id, and to which group the color should be added. * @param color the default color to add * @return whether it was successful. */ - bool addEntryWithDialog(ManagedColor *color); + // bool addEntryWithDialog(ManagedColor *color); /** * @brief addGroupWithDialog * gives a little dialog to ask for the desired groupname. * @return whether this was successful. */ - bool addGroupWithDialog(); + // bool addGroupWithDialog(); /** * @brief removeSelectedEntryWithDialog * removes the selected entry. If it is a group, it pop up a dialog * asking whether the colors should also be removed. * @return whether this was successful */ - bool removeSelectedEntryWithDialog(); + // bool removeSelectedEntryWithDialog(); /** * @brief trySelectClosestColor * tries to select the closest color to the one given. * It does not force a change on the active color. * @param color the color to compare to. */ void trySelectClosestColor(ManagedColor *color); Q_SIGNALS: /** * @brief entrySelectedForeGround * fires when a swatch is selected with leftclick. * @param entry */ void entrySelectedForeGround(KisSwatch entry); /** * @brief entrySelectedBackGround * fires when a swatch is selected with rightclick. * @param entry */ void entrySelectedBackGround(KisSwatch entry); private: struct Private; const QScopedPointer d; }; #endif // LIBKIS_PALETTE_VIEW_H diff --git a/libs/widgets/CMakeLists.txt b/libs/widgets/CMakeLists.txt index c2fdb1a1c8..bc6ed99758 100644 --- a/libs/widgets/CMakeLists.txt +++ b/libs/widgets/CMakeLists.txt @@ -1,146 +1,142 @@ add_subdirectory( tests ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(kritawidgets_LIB_SRCS KoGradientEditWidget.cpp KoVBox.cpp KoDialog.cpp KoZoomWidget.cpp KoTagToolButton.cpp KoTagChooserWidget.cpp KoTagFilterWidget.cpp KoResourceTaggingManager.cpp KoResourceItemChooserContextMenu.cpp KoAspectButton.cpp KoPagePreviewWidget.cpp KoSliderCombo.cpp KoColorPopupButton.cpp KoConfigAuthorPage.cpp KoUnitDoubleSpinBox.cpp KoZoomAction.cpp KoZoomController.cpp KoZoomInput.cpp KoZoomHandler.cpp KoZoomMode.cpp KoDpi.cpp KoColorPatch.cpp KoColorPopupAction.cpp KoColorSetWidget.cpp KoColorSlider.cpp KoTriangleColorSelector.cpp KoResourcePopupAction.cpp KoIconToolTip.cpp KoResourceItemChooser.cpp KoResourceItemChooserSync.cpp KoResourceSelector.cpp KoResourceModel.cpp KoResourceItemDelegate.cpp KoResourceItemView.cpp KoResourceTagStore.cpp KoRuler.cpp KoItemToolTip.cpp KoCheckerBoardPainter.cpp KoResourceServerAdapter.cpp KoResourceServerProvider.cpp KoLineStyleSelector.cpp KoLineStyleItemDelegate.cpp KoLineStyleModel.cpp KoResourceFiltering.cpp KoTitledTabWidget.cpp KoToolBoxButton.cpp KoToolBox.cpp KoToolBoxDocker.cpp KoToolBoxFactory.cpp KoToolDocker.cpp KoPageLayoutWidget.cpp KoPageLayoutDialog.cpp KoShadowConfigWidget.cpp KoMarkerSelector.cpp KoMarkerModel.cpp KoMarkerItemDelegate.cpp KoDocumentInfoDlg.cpp KoTableView.cpp WidgetsDebug.cpp kis_file_name_requester.cpp kis_double_parse_spin_box.cpp kis_double_parse_unit_spin_box.cpp kis_int_parse_spin_box.cpp KisColorSelectorInterface.cpp KoAnchorSelectionWidget.cpp squeezedcombobox.cpp KisGradientSlider.cpp KisGradientSliderWidget.cpp kis_color_input.cpp # classes used by internal color selector kis_spinbox_color_selector.cpp KisVisualColorSelector.cpp KisVisualColorSelectorShape.cpp KisVisualEllipticalSelectorShape.cpp KisVisualRectangleSelectorShape.cpp KisVisualTriangleSelectorShape.cpp KisScreenColorPickerBase.cpp KisDlgInternalColorSelector.cpp - KisPaletteListWidget.cpp - KisPaletteModel.cpp KisPaletteDelegate.cpp kis_palette_view.cpp + KisPaletteListWidget.cpp KisPaletteComboBox.cpp kis_popup_button.cc kis_color_button.cpp - - KisDlgPaletteEditor.cpp ) ki18n_wrap_ui( kritawidgets_LIB_SRCS KoConfigAuthorPage.ui koDocumentInfoAboutWidget.ui koDocumentInfoAuthorWidget.ui wdg_file_name_requester.ui KoPageLayoutWidget.ui KoShadowConfigWidget.ui WdgDlgInternalColorSelector.ui - WdgDlgPaletteEditor.ui WdgPaletteListWidget.ui ) add_library(kritawidgets SHARED ${kritawidgets_LIB_SRCS}) generate_export_header(kritawidgets BASE_NAME kritawidgets) target_link_libraries(kritawidgets kritaodf kritaglobal kritaflake kritapigment kritawidgetutils Qt5::PrintSupport KF5::CoreAddons KF5::ConfigGui KF5::GuiAddons KF5::WidgetsAddons KF5::ConfigCore KF5::Completion ) if(X11_FOUND) target_link_libraries(kritawidgets Qt5::X11Extras ${X11_LIBRARIES}) endif() set_target_properties(kritawidgets PROPERTIES VERSION ${GENERIC_KRITA_LIB_VERSION} SOVERSION ${GENERIC_KRITA_LIB_SOVERSION} ) install(TARGETS kritawidgets ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/libs/widgets/KisColorsetChooser.cpp b/libs/widgets/KisColorsetChooser.cpp deleted file mode 100644 index 6a0861c350..0000000000 --- a/libs/widgets/KisColorsetChooser.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* This file is part of the KDE project - * Copyright (C) 2013 Sven Langkamp - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#include "KisColorsetChooser.h" - -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "kis_int_parse_spin_box.h" - -class ColorSetDelegate : public QAbstractItemDelegate -{ -public: - ColorSetDelegate(QObject * parent = 0) : QAbstractItemDelegate(parent) {} - ~ColorSetDelegate() override {} - /// reimplemented - void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override; - /// reimplemented - QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const override { - return option.decorationSize; - } -}; - -void ColorSetDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const -{ - /* - painter->save(); - if (! index.isValid()) - return; - - KoResource* resource = static_cast(index.internalPointer()); - KoColorSet* colorSet = static_cast(resource); - - if (option.state & QStyle::State_Selected) { - painter->fillRect(option.rect, option.palette.highlight()); - painter->setPen(option.palette.highlightedText().color()); - } - else { - painter->setBrush(option.palette.text().color()); - } - painter->drawText(option.rect.x() + 5, option.rect.y() + painter->fontMetrics().ascent() + 5, colorSet->name()); - - int size = 7; - for (quint32 x = 0; x < colorSet->columnCount(); x++) { - for (quint32 y = 0; y < colorSet->rowCount(); y++) { - QRect rect(option.rect.x() + x * size, - option.rect.y() + option.rect.height() - size, - size, - size); - painter->fillRect(rect, colorSet->getColorGlobal(x, y).color().toQColor()); - } - } - - painter->restore(); - */ -} - -KisColorsetChooser::KisColorsetChooser(QWidget* parent): QWidget(parent) -{ - KoResourceServer * rserver = KoResourceServerProvider::instance()->paletteServer(); - QSharedPointer adapter(new KoResourceServerAdapter(rserver)); - m_itemChooser = new KoResourceItemChooser(adapter, this); - m_itemChooser->setItemDelegate(new ColorSetDelegate(this)); - m_itemChooser->showTaggingBar(true); - m_itemChooser->setFixedSize(250, 250); - m_itemChooser->setRowHeight(30); - m_itemChooser->setColumnCount(1); - connect(m_itemChooser, SIGNAL(resourceSelected(KoResource*)), - this, SLOT(resourceSelected(KoResource*))); - - KConfigGroup cfg = KSharedConfig::openConfig()->group(""); - m_itemChooser->configureKineticScrolling(cfg.readEntry("KineticScrollingGesture", 0), - cfg.readEntry("KineticScrollingSensitivity", 75), - cfg.readEntry("KineticScrollingScrollbar", true)); - - QPushButton* saveButton = new QPushButton(i18n("Save")); - connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(slotSave())); - - m_nameEdit = new QLineEdit(this); - m_nameEdit->setPlaceholderText(i18n("Insert name")); - m_nameEdit->setClearButtonEnabled(true); - - m_columnEdit = new KisIntParseSpinBox(this); - m_columnEdit->setRange(1, 30); - m_columnEdit->setValue(10); - - QGridLayout* layout = new QGridLayout(this); - layout->addWidget(m_itemChooser, 0, 0, 1, 3); - layout->setColumnStretch(1, 1); - layout->addWidget(saveButton, 2, 2, 1, 1); - layout->addWidget(m_nameEdit, 1, 1, 1, 2); - layout->addWidget(new QLabel(i18n("Name:"), this), 1, 0, 1, 1); - layout->addWidget(m_columnEdit, 2, 1, 1, 1); - layout->addWidget(new QLabel(i18n("Columns:"), this), 2, 0, 1, 1); -} - -KisColorsetChooser::~KisColorsetChooser() -{ -} - -void KisColorsetChooser::resourceSelected(KoResource* resource) -{ - emit paletteSelected(static_cast(resource)); -} - -void KisColorsetChooser::slotSave() -{ - KoResourceServer * rserver = KoResourceServerProvider::instance()->paletteServer(); - - KoColorSet* colorset = new KoColorSet(); - colorset->setValid(true); - - QString saveLocation = rserver->saveLocation(); - QString name = m_nameEdit->text(); - int columns = m_columnEdit->value(); - - bool newName = false; - if(name.isEmpty()) { - newName = true; - name = i18n("Palette"); - } - QFileInfo fileInfo(saveLocation + name + colorset->defaultFileExtension()); - - int i = 1; - while (fileInfo.exists()) { - fileInfo.setFile(saveLocation + name + QString("%1").arg(i) + colorset->defaultFileExtension()); - i++; - } - colorset->setFilename(fileInfo.filePath()); - if(newName) { - name = i18n("Palette %1", i); - } - colorset->setName(name); - colorset->setColumnCount(columns); - rserver->addResource(colorset); -} diff --git a/libs/widgets/KisColorsetChooser.h b/libs/widgets/KisColorsetChooser.h deleted file mode 100644 index f51cdda0f6..0000000000 --- a/libs/widgets/KisColorsetChooser.h +++ /dev/null @@ -1,55 +0,0 @@ -/* This file is part of the KDE project - * Copyright (C) 2013 Sven Langkamp - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - - -#ifndef KIS_COLORSET_CHOOSER_H -#define KIS_COLORSET_CHOOSER_H - -#include - -class QSpinBox; -class KoColorSet; -class QLineEdit; -class KoResourceItemChooser; -class KoResource; - -#include "kritawidgets_export.h" - - -class KRITAWIDGETS_EXPORT KisColorsetChooser : public QWidget -{ - Q_OBJECT -public: - KisColorsetChooser(QWidget* parent = 0); - ~KisColorsetChooser() override; - -Q_SIGNALS: - void paletteSelected(KoColorSet* colorSet); - -private Q_SLOTS: - void resourceSelected(KoResource* resource); - void slotSave(); - -private: - KoResourceItemChooser * m_itemChooser; - QLineEdit* m_nameEdit; - QSpinBox* m_columnEdit; -}; - -#endif // COLORSET_CHOOSER_H diff --git a/libs/widgets/KisDlgPaletteEditor.cpp b/libs/widgets/KisDlgPaletteEditor.cpp deleted file mode 100644 index 91b80e0b3f..0000000000 --- a/libs/widgets/KisDlgPaletteEditor.cpp +++ /dev/null @@ -1,261 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "KisDlgPaletteEditor.h" - -KisDlgPaletteEditor::KisDlgPaletteEditor() - : m_ui(new Ui_WdgDlgPaletteEditor) - , m_actAddGroup(new QAction(i18n("Add a group"))) - , m_actDelGroup(new QAction(i18n("Delete this group"))) - , m_actRenGroup(new QAction(i18n("Rename this group"))) - , m_currentGroupOriginalName(KoColorSet::GLOBAL_GROUP_NAME) -{ - m_ui->setupUi(this); - m_ui->gbxPalette->setTitle(i18n("Palette settings")); - m_ui->labelFilename->setText(i18n("Filename")); - m_ui->labelName->setText(i18n("Palette Name")); - m_ui->bnAddGroup->setDefaultAction(m_actAddGroup.data()); - - m_ui->gbxGroup->setTitle(i18n("Group settings")); - m_ui->labelColCount->setText(i18n("Column count")); - m_ui->labelRowCount->setText(i18n("Row count")); - m_ui->bnDelGroup->setDefaultAction(m_actDelGroup.data()); - m_ui->bnRenGroup->setDefaultAction(m_actRenGroup.data()); - - connect(m_actAddGroup.data(), SIGNAL(triggered(bool)), SLOT(slotAddGroup())); - connect(m_actDelGroup.data(), SIGNAL(triggered(bool)), SLOT(slotDelGroup())); - connect(m_actRenGroup.data(), SIGNAL(triggered(bool)), SLOT(slotRenGroup())); - connect(m_ui->spinBoxRow, SIGNAL(valueChanged(int)), SLOT(slotRowCountChanged(int))); -} - -KisDlgPaletteEditor::~KisDlgPaletteEditor() -{ } - -void KisDlgPaletteEditor::setPalette(KoColorSet *colorSet) -{ - m_colorSet = colorSet; - if (m_colorSet.isNull()) { - return; - } - m_original.reset(new OriginalPaletteInfo); - m_ui->lineEditName->setText(m_colorSet->name()); - m_ui->lineEditFilename->setText(m_colorSet->filename()); - m_ui->spinBoxCol->setValue(m_colorSet->columnCount()); - m_ui->ckxGlobal->setCheckState(m_colorSet->isGlobal() ? Qt::Checked : Qt::Unchecked); - m_ui->ckxReadOnly->setCheckState(!m_colorSet->isEditable() ? Qt::Checked : Qt::Unchecked); - m_original->name = m_colorSet->name(); - m_original->filename = m_colorSet->filename(); - m_original->columnCount = m_colorSet->columnCount(); - m_original->isGlobal = m_colorSet->isGlobal(); - m_original->isReadOnly = !m_colorSet->isEditable(); - - Q_FOREACH (const QString & groupName, m_colorSet->getGroupNames()) { - KisSwatchGroup *group = m_colorSet->getGroup(groupName); - m_groups[groupName] = GroupInfo(groupName, group->rowCount()); - m_original->groups[groupName] = GroupInfo(groupName, group->rowCount()); - m_ui->cbxGroup->addItem(groupName); - } - connect(m_ui->cbxGroup, SIGNAL(currentTextChanged(QString)), SLOT(slotGroupChosen(QString))); - m_ui->cbxGroup->setCurrentText(KoColorSet::GLOBAL_GROUP_NAME); - m_ui->bnDelGroup->setEnabled(false); - m_ui->bnRenGroup->setEnabled(false); - - m_ui->spinBoxRow->setValue(m_groups[KoColorSet::GLOBAL_GROUP_NAME].rowNumber); - - bool canWrite = m_colorSet->isEditable(); - m_ui->lineEditName->setEnabled(canWrite); - m_ui->lineEditFilename->setEnabled(canWrite); - m_ui->spinBoxCol->setEnabled(canWrite); - m_ui->spinBoxRow->setEnabled(canWrite); - m_ui->ckxGlobal->setEnabled(canWrite); - m_ui->ckxReadOnly->setEnabled(canWrite); - m_ui->bnAddGroup->setEnabled(canWrite); -} - -QString KisDlgPaletteEditor::name() const -{ - return m_ui->lineEditName->text(); -} - -QString KisDlgPaletteEditor::filename() const -{ - return m_ui->lineEditFilename->text(); -} - -const QSet &KisDlgPaletteEditor::newGroupNames() const -{ - return m_newGroups; -} - -int KisDlgPaletteEditor::columnCount() const -{ - return m_ui->spinBoxCol->value(); -} - -bool KisDlgPaletteEditor::isGlobal() const -{ - return m_ui->ckxGlobal->checkState() == Qt::Checked; -} - -bool KisDlgPaletteEditor::isReadOnly() const -{ - return m_ui->ckxReadOnly->checkState() == Qt::Checked; -} - -bool KisDlgPaletteEditor::isModified() const -{ - Q_ASSERT(!m_original.isNull()); - return m_original->isReadOnly != isReadOnly() || - m_original->isGlobal != isGlobal() || - m_original->name != name() || - m_original->filename != filename() || - m_original->columnCount != columnCount() || - m_original->groups != m_groups; -} - -bool KisDlgPaletteEditor::groupRemoved(const QString &groupName) const -{ - if (groupName == KoColorSet::GLOBAL_GROUP_NAME) { return false; } - return m_original->groups.contains(groupName) && !m_groups.contains(groupName); -} - -QString KisDlgPaletteEditor::groupRenamedTo(const QString &oriGroupName) const -{ - if (!m_groups.contains(oriGroupName) || m_groups[oriGroupName].newName == oriGroupName) { - return QString(); - } - return m_groups[oriGroupName].newName; -} - -void KisDlgPaletteEditor::slotAddGroup() -{ - KoDialog dlg; - QVBoxLayout layout(&dlg); - dlg.mainWidget()->setLayout(&layout); - QLabel lblName(i18n("Name"), &dlg); - layout.addWidget(&lblName); - QLineEdit leName(&dlg); - layout.addWidget(&leName); - QLabel lblRowCount(i18n("Row count"), &dlg); - layout.addWidget(&lblRowCount); - QSpinBox spxRow(&dlg); - spxRow.setValue(20); - layout.addWidget(&spxRow); - if (dlg.exec() != QDialog::Accepted) { return; } - if (m_colorSet->getGroup(leName.text())) { - QMessageBox msgNameDuplicate; - msgNameDuplicate.setText(i18n("Group already exists")); - msgNameDuplicate.setWindowTitle(i18n("Group already exists! Group not added.")); - msgNameDuplicate.exec(); - return; - } - m_groups.insert(leName.text(), GroupInfo(leName.text(), spxRow.value())); - m_newGroups.insert(leName.text()); - m_ui->cbxGroup->addItem(leName.text()); - m_ui->cbxGroup->setCurrentIndex(m_ui->cbxGroup->count() - 1); -} - -void KisDlgPaletteEditor::slotRenGroup() -{ - KoDialog dlg; - QFormLayout form(&dlg); - dlg.mainWidget()->setLayout(&form); - QLineEdit leNewName; - leNewName.setText(m_groups[m_currentGroupOriginalName].newName); - form.addRow(i18nc("Renaming swatch group", "New name"), &leNewName); - if (dlg.exec() != KoDialog::Accepted) { return; } - if (leNewName.text().isEmpty()) { return; } - if (m_groups.contains(leNewName.text())) { return; } - m_groups[m_currentGroupOriginalName].newName = leNewName.text(); - if (m_newGroups.remove(m_currentGroupOriginalName)) { - m_newGroups.insert(leNewName.text()); - } - int idx = m_ui->cbxGroup->currentIndex(); - m_ui->cbxGroup->removeItem(idx); - m_ui->cbxGroup->insertItem(idx, leNewName.text()); - m_ui->cbxGroup->setCurrentIndex(idx); -} - -void KisDlgPaletteEditor::slotDelGroup() -{ - if (m_currentGroupOriginalName == KoColorSet::GLOBAL_GROUP_NAME) { - QMessageBox msgNameDuplicate; - msgNameDuplicate.setText(i18n("Can't delete group")); - msgNameDuplicate.setWindowTitle(i18n("Can't delete the main group of a palette.")); - msgNameDuplicate.exec(); - return; - } - - KoDialog window(this); - window.setWindowTitle(i18nc("@title:window", "Removing Group")); - QFormLayout editableItems(&window); - QCheckBox chkKeep(&window); - window.mainWidget()->setLayout(&editableItems); - editableItems.addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), &chkKeep); - if (window.exec() != KoDialog::Accepted) { return; } - if (chkKeep.isChecked()) { - m_keepColorGroups.insert(m_currentGroupOriginalName); - } - - QString deletedName = m_currentGroupOriginalName; - m_ui->cbxGroup->setCurrentIndex(0); - m_ui->cbxGroup->removeItem(m_ui->cbxGroup->findText(m_groups[deletedName].newName)); - m_groups.remove(deletedName); - m_newGroups.remove(deletedName); -} - -void KisDlgPaletteEditor::slotGroupChosen(const QString &groupName) -{ - if (groupName == KoColorSet::GLOBAL_GROUP_NAME) { - m_ui->bnDelGroup->setEnabled(false); - m_ui->bnRenGroup->setEnabled(false); - } else { - m_ui->bnDelGroup->setEnabled(true); - m_ui->bnRenGroup->setEnabled(true); - } - m_currentGroupOriginalName = oldNameFromNewName(groupName); - m_ui->spinBoxRow->setValue(m_groups[m_currentGroupOriginalName].rowNumber); -} - -int KisDlgPaletteEditor::groupRowNumber(const QString &groupName) const -{ - return m_groups[groupName].rowNumber; -} - -bool KisDlgPaletteEditor::groupKeepColors(const QString &groupName) const -{ - return m_keepColorGroups.contains(groupName); -} - -void KisDlgPaletteEditor::slotRowCountChanged(int newCount) -{ - m_groups[m_currentGroupOriginalName].rowNumber = newCount; -} - -QString KisDlgPaletteEditor::oldNameFromNewName(const QString &newName) const -{ - Q_FOREACH (const QString &oldGroupName, m_groups.keys()) { - if (m_groups[oldGroupName].newName == newName) { - return oldGroupName; - } - } - return QString(); -} - -bool operator ==(const KisDlgPaletteEditor::GroupInfo &lhs, const KisDlgPaletteEditor::GroupInfo &rhs) -{ - return lhs.newName == rhs.newName && lhs.rowNumber == rhs.rowNumber; -} diff --git a/libs/widgets/KisDlgPaletteEditor.h b/libs/widgets/KisDlgPaletteEditor.h deleted file mode 100644 index 5d086c7735..0000000000 --- a/libs/widgets/KisDlgPaletteEditor.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef KISDLGPALETTEEDITOR_H -#define KISDLGPALETTEEDITOR_H - -#include -#include -#include -#include -#include -#include - -class QAction; - -class KoColorSet; -class KisSwatchGroup; - -class Ui_WdgDlgPaletteEditor; - -class KisDlgPaletteEditor : public QDialog -{ - Q_OBJECT -private: - struct GroupInfo { - GroupInfo() { } - GroupInfo(const QString &n, int r) - : newName(n) - , rowNumber(r) - { } - QString newName; - int rowNumber; - }; - struct OriginalPaletteInfo { - QString name; - QString filename; - int columnCount; - bool isGlobal; - bool isReadOnly; - QHash groups; - }; - -public: - explicit KisDlgPaletteEditor(); - ~KisDlgPaletteEditor(); - -public: - void setPalette(KoColorSet *); - KoColorSet* palette() const { return m_colorSet; } - - QString name() const; - QString filename() const; - const QSet &newGroupNames() const; - - int columnCount() const; - bool isGlobal() const; - bool isReadOnly() const; - bool isModified() const; - /** - * @brief groupRemoved - * @param groupName original group name - * @return if the group is removed - */ - bool groupRemoved(const QString &groupName) const; - /** - * @brief groupRenamedTo - * @param groupName original group name - * @return new group name if renamed; emptry string if not renamed or not - * allowed to be renamed - */ - QString groupRenamedTo(const QString &oriGroupName) const; - /** - * @brief groupRowNumber - * @param groupName ORIGINAL group name - * @return new group row number of the group - */ - int groupRowNumber(const QString &groupName) const; - bool groupKeepColors(const QString &groupName) const; - -private Q_SLOTS: - void slotDelGroup(); - void slotAddGroup(); - void slotRenGroup(); - - void slotGroupChosen(const QString &groupName); - void slotRowCountChanged(int); - -private: - QString oldNameFromNewName(const QString &newName) const; - -private: - QScopedPointer m_ui; - QScopedPointer m_actAddGroup; - QScopedPointer m_actDelGroup; - QScopedPointer m_actRenGroup; - QPointer m_colorSet; - QScopedPointer m_original; - QHash m_groups; // key is original group name - QSet m_newGroups; - QSet m_keepColorGroups; - QString m_currentGroupOriginalName; - -private: - friend bool operator ==(const GroupInfo &lhs, const GroupInfo &rhs); -}; - -#endif // KISDLGPALETTEEDITOR_H diff --git a/libs/widgets/KisPaletteListWidget.cpp b/libs/widgets/KisPaletteListWidget.cpp index b7b30b932f..a1dd48c885 100644 --- a/libs/widgets/KisPaletteListWidget.cpp +++ b/libs/widgets/KisPaletteListWidget.cpp @@ -1,296 +1,178 @@ #include #include #include #include #include #include #include #include #include #include #include -#include "KisDlgPaletteEditor.h" - #include #include "KisPaletteListWidget.h" #include "KisPaletteListWidget_p.h" KisPaletteListWidget::KisPaletteListWidget(QWidget *parent) : QWidget(parent) , m_ui(new Ui_WdgPaletteListWidget) , m_d(new KisPaletteListWidgetPrivate(this)) { m_d->allowModification = false; m_d->actAdd.reset(new QAction(KisIconUtils::loadIcon("list-add"), i18n("Add a new palette"))); m_d->actRemove.reset(new QAction(KisIconUtils::loadIcon("list-remove"), i18n("Remove current palette"))); - m_d->actModify.reset(new QAction(KisIconUtils::loadIcon("edit-rename"), - i18n("Rename choosen palette"))); m_d->actImport.reset(new QAction(KisIconUtils::loadIcon("document-import"), i18n("Import a new palette from file"))); m_d->actExport.reset(new QAction(KisIconUtils::loadIcon("document-export"), i18n("Export current palette to file"))); m_d->model->setColumnCount(1); m_ui->setupUi(this); m_ui->bnAdd->setDefaultAction(m_d->actAdd.data()); m_ui->bnRemove->setDefaultAction(m_d->actRemove.data()); - m_ui->bnEdit->setDefaultAction(m_d->actModify.data()); m_ui->bnImport->setDefaultAction(m_d->actImport.data()); m_ui->bnExport->setDefaultAction(m_d->actExport.data()); + m_ui->bnAdd->setEnabled(false); + m_ui->bnRemove->setEnabled(false); + m_ui->bnImport->setEnabled(false); + m_ui->bnExport->setEnabled(false); + connect(m_d->actAdd.data(), SIGNAL(triggered()), SLOT(slotAdd())); connect(m_d->actRemove.data(), SIGNAL(triggered()), SLOT(slotRemove())); - connect(m_d->actModify.data(), SIGNAL(triggered()), SLOT(slotModify())); connect(m_d->actImport.data(), SIGNAL(triggered()), SLOT(slotImport())); connect(m_d->actExport.data(), SIGNAL(triggered()), SLOT(slotExport())); m_d->itemChooser->setItemDelegate(m_d->delegate.data()); m_d->itemChooser->setRowHeight(40); m_d->itemChooser->setColumnCount(1); m_d->itemChooser->showButtons(false); m_d->itemChooser->showTaggingBar(true); m_ui->viewPalette->setLayout(new QHBoxLayout(m_ui->viewPalette)); m_ui->viewPalette->layout()->addWidget(m_d->itemChooser.data()); connect(m_d->itemChooser.data(), SIGNAL(resourceSelected(KoResource *)), SLOT(slotPaletteResourceSelected(KoResource*))); m_d->itemChooser->setCurrentItem(0, 0); } KisPaletteListWidget::~KisPaletteListWidget() { } void KisPaletteListWidget::slotPaletteResourceSelected(KoResource *r) { KoColorSet *g = static_cast(r); + emit sigPaletteSelected(g); + if (!m_d->allowModification) { return; } if (g->isEditable()) { m_ui->bnAdd->setEnabled(true); m_ui->bnRemove->setEnabled(true); - m_ui->bnEdit->setEnabled(true); } else { m_ui->bnAdd->setEnabled(false); m_ui->bnRemove->setEnabled(false); - m_ui->bnEdit->setEnabled(false); } - emit sigPaletteSelected(g); } void KisPaletteListWidget::slotAdd() { - KoColorSet *newColorSet = new KoColorSet(newPaletteFileName()); - newColorSet->setPaletteType(KoColorSet::KPL); - newColorSet->setIsGlobal(false); - newColorSet->setIsEditable(true); - newColorSet->setName("New Palette"); - m_d->rAdapter->addResource(newColorSet); - m_d->itemChooser->setCurrentResource(newColorSet); - - emit sigPaletteListChanged(); + if (!m_d->allowModification) { return; } + emit sigAddPalette(); + m_d->itemChooser->setCurrentItem(m_d->rAdapter->resources().size()-1, 0); } void KisPaletteListWidget::slotRemove() { + if (!m_d->allowModification) { return; } if (m_d->itemChooser->currentResource()) { KoColorSet *cs = static_cast(m_d->itemChooser->currentResource()); - if (!cs || !cs->isEditable()) { - return; - } - if (cs->isGlobal()) { - QFile::remove(cs->filename()); - } - m_d->rAdapter->removeResource(cs); + emit sigRemovePalette(cs); } m_d->itemChooser->setCurrentItem(0, 0); - - emit sigPaletteListChanged(); -} - -void KisPaletteListWidget::slotModify() -{ - KisDlgPaletteEditor dlg; - KoColorSet *colorSet = static_cast(m_d->itemChooser->currentResource()); - if (!colorSet) { return; } - dlg.setPalette(colorSet); - if (dlg.exec() != QDialog::Accepted){ return; } - if (!dlg.isModified()) { return; } - colorSet->setName(dlg.name()); - colorSet->setColumnCount(dlg.columnCount()); - if (dlg.isGlobal()) { - setPaletteGlobal(colorSet); - } else { - setPaletteNonGlobal(colorSet); - } - Q_FOREACH (const QString &newGroupName, dlg.newGroupNames()) { - qDebug() << "new group:" << newGroupName; - colorSet->addGroup(newGroupName); - colorSet->getGroup(newGroupName)->setRowCount(dlg.groupRowNumber(newGroupName)); - } - Q_FOREACH (const QString &groupName, colorSet->getGroupNames()) { - qDebug() << "modifying existing" << groupName; - colorSet->getGroup(groupName)->setRowCount(dlg.groupRowNumber(groupName)); - if (groupName != KoColorSet::GLOBAL_GROUP_NAME) { continue; } - if (dlg.groupRemoved(groupName)) { - colorSet->removeGroup(groupName, dlg.groupKeepColors(groupName)); - continue; - } - if (!dlg.groupRenamedTo(groupName).isEmpty()) { - qDebug() << "group renamed:" << groupName; - qDebug() << "to:" << dlg.groupRenamedTo(groupName); - colorSet->changeGroupName(groupName, dlg.groupRenamedTo(groupName)); - } - } - Q_FOREACH (const KoResource *r, m_d->rAdapter->resources()) { - if (r != colorSet && r->filename() == dlg.filename()) { - QMessageBox msgFilenameDuplicate; - msgFilenameDuplicate.setWindowTitle(i18n("Duplicate filename")); - msgFilenameDuplicate.setText(i18n("Duplicate filename! Palette not saved.")); - msgFilenameDuplicate.exec(); - return; - } - } - colorSet->setFilename(dlg.filename()); - emit sigPaletteSelected(colorSet); // to update elements in the docker - emit sigPaletteListChanged(); } void KisPaletteListWidget::slotImport() { - KoFileDialog dialog(this, KoFileDialog::OpenFile, "Open Palette"); - dialog.setDefaultDir(QDir::homePath()); - dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset" << "application/x-gimp-color-palette"); - QString fileName = dialog.filename(); - if (fileName.isEmpty()) { return; } - KoColorSet *colorSet = new KoColorSet(fileName); - colorSet->load(); - m_d->rAdapter->addResource(colorSet); - m_d->itemChooser->setCurrentResource(colorSet); - emit sigPaletteListChanged(); + if (!m_d->allowModification) { return; } + emit sigImportPalette(); + m_d->itemChooser->setCurrentItem(m_d->rAdapter->resources().size()-1, 0); } void KisPaletteListWidget::slotExport() { - KoColorSet *r = static_cast(m_d->itemChooser->currentResource()); - KoFileDialog dialog(this, KoFileDialog::SaveFile, "Save Palette"); - dialog.setDefaultDir(r->filename()); - dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset"); - QString newPath; - bool isStandAlone = r->isGlobal(); - QString oriPath = r->filename(); - if ((newPath = dialog.filename()).isEmpty()) { return; } - r->setFilename(newPath); - r->setIsGlobal(true); - r->save(); - r->setFilename(oriPath); - r->setIsGlobal(isStandAlone); -} - -void KisPaletteListWidget::setPaletteGlobal(KoColorSet *colorSet) -{ - if (QPointer(colorSet).isNull()) { return; } - KoResourceServer *rserver = KoResourceServerProvider::instance()->paletteServer(); - - QString saveLocation = rserver->saveLocation(); - QString name = colorSet->filename(); - - QFileInfo fileInfo(saveLocation + name); - - colorSet->setFilename(fileInfo.filePath()); - colorSet->setIsGlobal(true); -} - -void KisPaletteListWidget::setPaletteNonGlobal(KoColorSet *colorSet) -{ - if (QPointer(colorSet).isNull()) { return; } - QString filename = newPaletteFileName(); - QFile::remove(colorSet->filename()); - colorSet->setFilename(filename); - colorSet->setIsGlobal(false); + if (!m_d->allowModification) { return; } + emit sigExportPalette(static_cast(m_d->itemChooser->currentResource())); } void KisPaletteListWidget::setAllowModification(bool allowModification) { m_d->allowModification = allowModification; + m_ui->bnAdd->setEnabled(allowModification); m_ui->bnImport->setEnabled(allowModification); m_ui->bnExport->setEnabled(allowModification); -} - -QString KisPaletteListWidget::newPaletteFileName() -{ - KoColorSet tmpColorSet; - QString result = "new_palette_"; - QSet nameSet; - QList rlist = m_d->rAdapter->resources(); - Q_FOREACH (const KoResource *r, rlist) { - nameSet.insert(r->filename()); - } - int i = 0; - while (nameSet.contains(result + QString::number(i) + tmpColorSet.defaultFileExtension())) { - i++; - } - result = result + QString::number(i) + tmpColorSet.defaultFileExtension(); - return result; + KoColorSet *cs = static_cast(m_d->itemChooser->currentResource()); + m_ui->bnRemove->setEnabled(allowModification && cs && cs->isEditable()); } /************************* KisPaletteListWidgetPrivate **********************/ KisPaletteListWidgetPrivate::KisPaletteListWidgetPrivate(KisPaletteListWidget *a_c) : c(a_c) , rAdapter(new KoResourceServerAdapter(KoResourceServerProvider::instance()->paletteServer())) , itemChooser(new KoResourceItemChooser(rAdapter, a_c)) , model(new Model(rAdapter, a_c)) , delegate(new Delegate(a_c)) { } KisPaletteListWidgetPrivate::~KisPaletteListWidgetPrivate() { } /******************* KisPaletteListWidgetPrivate::Delegate ******************/ KisPaletteListWidgetPrivate::Delegate::Delegate(QObject *parent) : QAbstractItemDelegate(parent) { } KisPaletteListWidgetPrivate::Delegate::~Delegate() { } void KisPaletteListWidgetPrivate::Delegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { painter->save(); if (!index.isValid()) return; KoResource* resource = static_cast(index.internalPointer()); KoColorSet* colorSet = static_cast(resource); QRect previewRect(option.rect.x() + 2, option.rect.y() + 2, option.rect.height() - 4, option.rect.height() - 4); painter->drawImage(previewRect, colorSet->image()); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); painter->drawImage(previewRect, colorSet->image()); painter->setPen(option.palette.highlightedText().color()); } else { painter->setBrush(option.palette.text().color()); } painter->drawText(option.rect.x() + previewRect.width() + 10, option.rect.y() + painter->fontMetrics().ascent() + 5, colorSet->name()); painter->restore(); } inline QSize KisPaletteListWidgetPrivate::Delegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const { return option.decorationSize; } diff --git a/libs/widgets/KisPaletteListWidget.h b/libs/widgets/KisPaletteListWidget.h index 336d9246e0..5bce77b70f 100644 --- a/libs/widgets/KisPaletteListWidget.h +++ b/libs/widgets/KisPaletteListWidget.h @@ -1,49 +1,49 @@ #ifndef KISPALETTECHOOSER_H #define KISPALETTECHOOSER_H #include #include #include #include "kritawidgets_export.h" class KoResource; class KoColorSet; class KisPaletteListWidgetPrivate; class KRITAWIDGETS_EXPORT KisPaletteListWidget : public QWidget { Q_OBJECT public: explicit KisPaletteListWidget(QWidget *parent = nullptr); virtual ~KisPaletteListWidget(); public: void setAllowModification(bool allowModification); Q_SIGNALS: void sigPaletteSelected(KoColorSet*); - void sigPaletteListChanged(); + void sigAddPalette(); + void sigRemovePalette(KoColorSet *); + void sigImportPalette(); + void sigExportPalette(KoColorSet *); public Q_SLOTS: private /* methods */: - void setPaletteGlobal(KoColorSet *colorSet); - void setPaletteNonGlobal(KoColorSet *colorSet); QString newPaletteFileName(); private Q_SLOTS: void slotPaletteResourceSelected(KoResource *); void slotAdd(); void slotRemove(); - void slotModify(); void slotImport(); void slotExport(); private: QScopedPointer m_ui; QScopedPointer m_d; }; #endif // KISPALETTECHOOSER_H diff --git a/libs/widgets/KisPaletteModel.cpp b/libs/widgets/KisPaletteModel.cpp index 52dfb29e7e..9a685c6db7 100644 --- a/libs/widgets/KisPaletteModel.cpp +++ b/libs/widgets/KisPaletteModel.cpp @@ -1,456 +1,473 @@ /* * Copyright (c) 2013 Sven Langkamp * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisPaletteModel.h" #include #include #include #include #include #include #include #include #include KisPaletteModel::KisPaletteModel(QObject* parent) : QAbstractTableModel(parent) , m_colorSet(0) , m_displayRenderer(KoDumbColorDisplayRenderer::instance()) { connect(this, SIGNAL(sigPaletteModifed()), SLOT(slotPaletteModified())); } KisPaletteModel::~KisPaletteModel() { } QVariant KisPaletteModel::data(const QModelIndex& index, int role) const { bool groupNameRow = m_groupNameRows.contains(index.row()); if (role == IsGroupNameRole) { return groupNameRow; } if (groupNameRow) { return dataForGroupNameRow(index, role); } else { return dataForSwatch(index, role); } } int KisPaletteModel::rowCount(const QModelIndex& /*parent*/) const { if (!m_colorSet) return 0; return m_colorSet->rowCount() // count of color rows + m_groupNameRows.size() // rows for names - 1; // global doesn't have a name } int KisPaletteModel::columnCount(const QModelIndex& /*parent*/) const { if (m_colorSet && m_colorSet->columnCount() > 0) { return m_colorSet->columnCount(); } if (!m_colorSet) { return 0; } return 16; } Qt::ItemFlags KisPaletteModel::flags(const QModelIndex& index) const { if (index.isValid()) { return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; } return Qt::ItemIsDropEnabled; } QModelIndex KisPaletteModel::index(int row, int column, const QModelIndex& parent) const { Q_UNUSED(parent); Q_ASSERT(m_colorSet); int groupNameRow = groupNameRowForRow(row); KisSwatchGroup *group = m_colorSet->getGroup(m_groupNameRows[groupNameRow]); Q_ASSERT(group); return createIndex(row, column, group); } void KisPaletteModel::setColorSet(KoColorSet* colorSet) { beginResetModel(); m_groupNameRows.clear(); m_colorSet = colorSet; if (colorSet) { int row = -1; for (const QString &groupName : m_colorSet->getGroupNames()) { m_groupNameRows[row] = groupName; row += m_colorSet->getGroup(groupName)->rowCount(); row += 1; // row for group name } } endResetModel(); + + emit sigPaletteChanged(); } KoColorSet* KisPaletteModel::colorSet() const { return m_colorSet; } int KisPaletteModel::rowNumberInGroup(int rowInModel) const { if (m_groupNameRows.contains(rowInModel)) { return -1; } for (auto it = m_groupNameRows.keys().rbegin(); it != m_groupNameRows.keys().rend(); it++) { if (*it < rowInModel) { return rowInModel - *it - 1; } } return rowInModel; } bool KisPaletteModel::addEntry(const KisSwatch &entry, const QString &groupName) { beginResetModel(); m_colorSet->add(entry, groupName); endResetModel(); if (m_colorSet->isGlobal()) { m_colorSet->save(); } emit sigPaletteModifed(); return true; } bool KisPaletteModel::removeEntry(const QModelIndex &index, bool keepColors) { if (!qvariant_cast(data(index, IsGroupNameRole))) { static_cast(index.internalPointer())->removeEntry(index.column(), rowNumberInGroup(index.row())); emit dataChanged(index, index); } else { int groupNameRow = groupNameRowForRow(index.row()); QString groupName = m_groupNameRows[groupNameRow]; - beginResetModel(); - m_colorSet->removeGroup(groupName, keepColors); - m_groupNameRows.clear(); - int row = -1; - for (const QString &groupName : m_colorSet->getGroupNames()) { - m_groupNameRows[row] = groupName; - row += m_colorSet->getGroup(groupName)->rowCount(); - row += 1; // row for group name - } - endResetModel(); + removeGroup(groupName, keepColors); } emit sigPaletteModifed(); return true; } +void KisPaletteModel::removeGroup(const QString &groupName, bool keepColors) +{ + beginResetModel(); + m_colorSet->removeGroup(groupName, keepColors); + m_groupNameRows.clear(); + int row = -1; + for (const QString &groupName : m_colorSet->getGroupNames()) { + m_groupNameRows[row] = groupName; + row += m_colorSet->getGroup(groupName)->rowCount(); + row += 1; // row for group name + } + endResetModel(); +} + bool KisPaletteModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { Q_UNUSED(row); Q_UNUSED(column); if (!data->hasFormat("krita/x-colorsetentry") && !data->hasFormat("krita/x-colorsetgroup")) { return false; } if (action == Qt::IgnoreAction) { return false; } if (data->hasFormat("krita/x-colorsetgroup")) { // dragging group not supported for now /* QByteArray encodedData = data->data("krita/x-colorsetgroup"); QDataStream stream(&encodedData, QIODevice::ReadOnly); while (!stream.atEnd()) { QString groupName; stream >> groupName; QModelIndex index = this->index(endRow, 0); if (index.isValid()) { QStringList entryList = qvariant_cast(index.data(RetrieveEntryRole)); QString groupDroppedOn = QString(); if (!entryList.isEmpty()) { groupDroppedOn = entryList.at(0); } int groupIndex = colorSet()->getGroupNames().indexOf(groupName); beginMoveRows( QModelIndex(), groupIndex, groupIndex, QModelIndex(), endRow); m_colorSet->moveGroup(groupName, groupDroppedOn); m_colorSet->save(); endMoveRows(); ++endRow; } */ return true; } QModelIndex finalIdx = parent; if (!finalIdx.isValid()) { return false; } if (qvariant_cast(finalIdx.data(KisPaletteModel::IsGroupNameRole))) { return true; } QByteArray encodedData = data->data("krita/x-colorsetentry"); QDataStream stream(&encodedData, QIODevice::ReadOnly); while (!stream.atEnd()) { KisSwatch entry; QString name, id; bool spotColor; QString oldGroupName; int oriRow; int oriColumn; QString colorXml; stream >> name >> id >> spotColor >> oriRow >> oriColumn >> oldGroupName >> colorXml; entry.setName(name); entry.setId(id); entry.setSpotColor(spotColor); QDomDocument doc; doc.setContent(colorXml); QDomElement e = doc.documentElement(); QDomElement c = e.firstChildElement(); if (!c.isNull()) { QString colorDepthId = c.attribute("bitdepth", Integer8BitsColorDepthID.id()); entry.setColor(KoColor::fromXML(c, colorDepthId)); } if (action == Qt::MoveAction){ KisSwatchGroup *g = m_colorSet->getGroup(oldGroupName); if (g) { if (qvariant_cast(finalIdx.data(KisPaletteModel::CheckSlotRole))) { g->setEntry(getEntry(finalIdx), oriColumn, oriRow); } else { g->removeEntry(oriColumn, oriRow); } } setEntry(entry, finalIdx); emit sigPaletteModifed(); if (m_colorSet->isGlobal()) { m_colorSet->save(); } } } return true; } QMimeData *KisPaletteModel::mimeData(const QModelIndexList &indexes) const { QMimeData *mimeData = new QMimeData(); QByteArray encodedData; QDataStream stream(&encodedData, QIODevice::WriteOnly); QString mimeTypeName = "krita/x-colorsetentry"; QModelIndex index = indexes.last(); if (index.isValid()) { if (qvariant_cast(index.data(IsGroupNameRole))==false) { KisSwatch entry = getEntry(index); QDomDocument doc; QDomElement root = doc.createElement("Color"); root.setAttribute("bitdepth", entry.color().colorSpace()->colorDepthId().id()); doc.appendChild(root); entry.color().toXML(doc, root); stream << entry.name() << entry.id() << entry.spotColor() << rowNumberInGroup(index.row()) << index.column() << qvariant_cast(index.data(KisPaletteModel::GroupNameRole)) << doc.toString(); } else { mimeTypeName = "krita/x-colorsetgroup"; QStringList entryList = qvariant_cast(index.data(RetrieveEntryRole)); QString groupName = QString(); if (!entryList.isEmpty()) { groupName = entryList.at(0); } stream << groupName; } } mimeData->setData(mimeTypeName, encodedData); return mimeData; } QStringList KisPaletteModel::mimeTypes() const { return QStringList() << "krita/x-colorsetentry" << "krita/x-colorsetgroup"; } Qt::DropActions KisPaletteModel::supportedDropActions() const { return Qt::MoveAction; } void KisPaletteModel::setEntry(const KisSwatch &entry, const QModelIndex &index) { KisSwatchGroup *group = static_cast(index.internalPointer()); Q_ASSERT(group); group->setEntry(entry, index.column(), rowNumberInGroup(index.row())); emit sigPaletteModifed(); emit dataChanged(index, index); if (m_colorSet->isGlobal()) { m_colorSet->save(); } } bool KisPaletteModel::renameGroup(const QString &groupName, const QString &newName) { beginResetModel(); bool success = m_colorSet->changeGroupName(groupName, newName); for (auto it = m_groupNameRows.begin(); it != m_groupNameRows.end(); it++) { if (it.value() == groupName) { m_groupNameRows[it.key()] = newName; break; } } endResetModel(); emit sigPaletteModifed(); return success; } +void KisPaletteModel::addGroup(const KisSwatchGroup &group) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount() + group.rowCount()); + m_colorSet->addGroup(group.name()); + *m_colorSet->getGroup(group.name()) = group; + endInsertColumns(); + + emit sigPaletteModifed(); +} + QVariant KisPaletteModel::dataForGroupNameRow(const QModelIndex &idx, int role) const { KisSwatchGroup *group = static_cast(idx.internalPointer()); Q_ASSERT(group); QString groupName = group->name(); switch (role) { case Qt::ToolTipRole: case Qt::DisplayRole: { return groupName; } case GroupNameRole: { return groupName; } case CheckSlotRole: { return true; } case RowInGroupRole: { return -1; } default: { return QVariant(); } } } QVariant KisPaletteModel::dataForSwatch(const QModelIndex &idx, int role) const { KisSwatchGroup *group = static_cast(idx.internalPointer()); Q_ASSERT(group); int rowInGroup = rowNumberInGroup(idx.row()); bool entryPresent = group->checkEntry(idx.column(), rowInGroup); KisSwatch entry; if (entryPresent) { entry = group->getEntry(idx.column(), rowInGroup); } switch (role) { case Qt::ToolTipRole: case Qt::DisplayRole: { return entryPresent ? entry.name() : i18n("Empty slot"); } case Qt::BackgroundRole: { QColor color(0, 0, 0, 0); if (entryPresent) { color = m_displayRenderer->toQColor(entry.color()); } return QBrush(color); } case GroupNameRole: { return group->name(); } case CheckSlotRole: { return entryPresent; } case RowInGroupRole: { return rowInGroup; } default: { return QVariant(); } } } void KisPaletteModel::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer) { if (displayRenderer) { if (m_displayRenderer) { disconnect(m_displayRenderer, 0, this, 0); } m_displayRenderer = displayRenderer; connect(m_displayRenderer, SIGNAL(displayConfigurationChanged()), SLOT(slotDisplayConfigurationChanged())); } else { m_displayRenderer = KoDumbColorDisplayRenderer::instance(); } } void KisPaletteModel::slotDisplayConfigurationChanged() { beginResetModel(); endResetModel(); } void KisPaletteModel::slotPaletteModified() { m_colorSet->setPaletteType(KoColorSet::KPL); } QModelIndex KisPaletteModel::indexForClosest(const KoColor &compare) { KisSwatchGroup::SwatchInfo info = colorSet()->getClosestColorInfo(compare); return createIndex(indexRowForInfo(info), info.column, colorSet()->getGroup(info.group)); } int KisPaletteModel::indexRowForInfo(const KisSwatchGroup::SwatchInfo &info) { for (auto it = m_groupNameRows.begin(); it != m_groupNameRows.end(); it++) { if (it.value() == info.group) { return it.key() + info.row + 1; } } return info.row; } KisSwatch KisPaletteModel::getEntry(const QModelIndex &index) const { KisSwatchGroup *group = static_cast(index.internalPointer()); if (!group || !group->checkEntry(index.column(), rowNumberInGroup(index.row()))) { return KisSwatch(); } return group->getEntry(index.column(), rowNumberInGroup(index.row())); } int KisPaletteModel::groupNameRowForRow(int rowInModel) const { return rowInModel - rowNumberInGroup(rowInModel) - 1; } diff --git a/libs/widgets/KisPaletteModel.h b/libs/widgets/KisPaletteModel.h index 73bd7f0819..9da3034fb9 100644 --- a/libs/widgets/KisPaletteModel.h +++ b/libs/widgets/KisPaletteModel.h @@ -1,166 +1,168 @@ /* * Copyright (c) 2013 Sven Langkamp * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KIS_PALETTEMODEL_H #define KIS_PALETTEMODEL_H #include #include #include #include #include "kritawidgets_export.h" #include #include class KoColorSet; class KisPaletteView; /** * @brief The KisPaletteModel class * This, together with KisPaletteView and KisPaletteDelegate forms a mvc way to access kocolorsets. * A display renderer is given to this model to convert KoColor to QColor when * colors are requested */ class KRITAWIDGETS_EXPORT KisPaletteModel : public QAbstractTableModel { Q_OBJECT public: explicit KisPaletteModel(QObject* parent = Q_NULLPTR); ~KisPaletteModel() override; enum AdditionalRoles { IsGroupNameRole = Qt::UserRole + 1, RetrieveEntryRole, CheckSlotRole, GroupNameRole, RowInGroupRole }; public /* overriden methods */: // QAbstractTableModel QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override; int columnCount(const QModelIndex& parent = QModelIndex()) const override; /** * @brief index * @param row * @param column * @param parent * @return the index of for the data at row, column * if the data is a color entry, the internal pointer points to the group * the entry belongs to, and the row and column are row number and column * number inside the group. * if the data is a group, the row number and group number is Q_INFINIFY, * and the internal pointer also points to the group */ QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; Qt::ItemFlags flags(const QModelIndex& index) const override; /** * @brief dropMimeData * This is an overridden function that handles dropped mimedata. * right now only colorsetentries and colorsetgroups are handled. * @return */ bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; /** * @brief mimeData * gives the mimedata for a kocolorsetentry or a kocolorsetgroup. * @param indexes * @return the mimedata for the given indices */ QMimeData *mimeData(const QModelIndexList &indexes) const override; QStringList mimeTypes() const override; Qt::DropActions supportedDropActions() const override; /** * @brief setData * setData is not used as KoColor is not a QVariant * use setEntry, addEntry and removeEntry instead */ // TODO Used QVariant::setValue and QVariant.value to implement this // bool setData(const QModelIndex &index, const QVariant &value, int role) override; Q_SIGNALS: void sigPaletteModifed(); + void sigPaletteChanged(); public /* methods */: /** * @brief addEntry * proper function to handle adding entries. * @return whether successful. */ bool addEntry(const KisSwatch &entry, const QString &groupName = KoColorSet::GLOBAL_GROUP_NAME); void setEntry(const KisSwatch &entry, const QModelIndex &index); /** * @brief removeEntry * proper function to remove the colorsetentry at the given index. * The consolidtes both removeentry and removegroup. * @param keepColors: This bool determines whether, when deleting a group, * the colors should be added to the default group. This is usually desirable, * so hence the default is true. * @return if successful */ bool removeEntry(const QModelIndex &index, bool keepColors=true); + void removeGroup(const QString &groupName, bool keepColors); + bool renameGroup(const QString &groupName, const QString &newName); + void addGroup(const KisSwatchGroup &group); KisSwatch getEntry(const QModelIndex &index) const; - bool renameGroup(const QString &groupName, const QString &newName); - void setColorSet(KoColorSet* colorSet); KoColorSet* colorSet() const; QModelIndex indexForClosest(const KoColor &compare); int indexRowForInfo(const KisSwatchGroup::SwatchInfo &info); public Q_SLOTS: private Q_SLOTS: void slotDisplayConfigurationChanged(); void slotPaletteModified(); private /* methods */: QVariant dataForGroupNameRow(const QModelIndex &idx, int role) const; QVariant dataForSwatch(const QModelIndex &idx, int role) const; int rowNumberInGroup(int rowInModel) const; int groupNameRowForRow(int rowInModel) const; /** * Installs a display renderer object for a palette that will * convert the KoColor to the displayable QColor. Default is the * dumb renderer. */ void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer); private /* member variables */: QPointer m_colorSet; QPointer m_displayRenderer; QMap m_groupNameRows; friend class KisPaletteView; }; #endif diff --git a/libs/widgets/WdgPaletteListWidget.ui b/libs/widgets/WdgPaletteListWidget.ui index cccbf3235e..ccc28a22f6 100644 --- a/libs/widgets/WdgPaletteListWidget.ui +++ b/libs/widgets/WdgPaletteListWidget.ui @@ -1,100 +1,92 @@ WdgPaletteListWidget 0 0 361 496 Form 0 0 0 0 Qt::Horizontal 40 20 .. - - - - - .. - - - .. .. .. diff --git a/libs/widgets/kis_palette_view.cpp b/libs/widgets/kis_palette_view.cpp index d7816bc40e..11911f1505 100644 --- a/libs/widgets/kis_palette_view.cpp +++ b/libs/widgets/kis_palette_view.cpp @@ -1,312 +1,261 @@ /* * Copyright (c) 2016 Dmitry Kazakov * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kis_palette_view.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "KisPaletteDelegate.h" #include "KisPaletteModel.h" #include "kis_color_button.h" #include int KisPaletteView::MININUM_ROW_HEIGHT = 10; struct KisPaletteView::Private { QPointer model; bool allowPaletteModification; // if modification is allowed from this widget }; KisPaletteView::KisPaletteView(QWidget *parent) : QTableView(parent) , m_d(new Private) { m_d->allowPaletteModification = false; setItemDelegate(new KisPaletteDelegate(this)); setShowGrid(true); setDropIndicatorShown(true); setDragDropMode(QAbstractItemView::InternalMove); setSelectionMode(QAbstractItemView::SingleSelection); setDragEnabled(false); setAcceptDrops(false); /* * without this, a cycle might be created: * the view streches to right border, and this make it need a scroll bar; * after the bar is added, the view shrinks to the bar, and this makes it * no longer need the bar any more, and the bar is removed again */ setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // set the size of swatches horizontalHeader()->setVisible(false); verticalHeader()->setVisible(false); horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); horizontalHeader()->setMinimumSectionSize(MININUM_ROW_HEIGHT); verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); verticalHeader()->setMinimumSectionSize(MININUM_ROW_HEIGHT); connect(horizontalHeader(), SIGNAL(sectionResized(int,int,int)), SLOT(slotHorizontalHeaderResized(int,int,int))); setAutoFillBackground(true); } KisPaletteView::~KisPaletteView() { } void KisPaletteView::setCrossedKeyword(const QString &value) { KisPaletteDelegate *delegate = dynamic_cast(itemDelegate()); KIS_ASSERT_RECOVER_RETURN(delegate); delegate->setCrossedKeyword(value); } bool KisPaletteView::addEntryWithDialog(KoColor color) { QScopedPointer window(new KoDialog(this)); window->setWindowTitle(i18nc("@title:window", "Add a new Colorset Entry")); QFormLayout *editableItems = new QFormLayout(window.data()); window->mainWidget()->setLayout(editableItems); QComboBox *cmbGroups = new QComboBox(window.data()); QString defaultGroupName = i18nc("Name for default group", "Default"); cmbGroups->addItem(defaultGroupName); cmbGroups->addItems(m_d->model->colorSet()->getGroupNames()); QLineEdit *lnIDName = new QLineEdit(window.data()); QLineEdit *lnName = new QLineEdit(window.data()); KisColorButton *bnColor = new KisColorButton(window.data()); QCheckBox *chkSpot = new QCheckBox(window.data()); chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color.")); editableItems->addRow(i18n("Group"), cmbGroups); editableItems->addRow(i18n("ID"), lnIDName); editableItems->addRow(i18n("Name"), lnName); editableItems->addRow(i18n("Color"), bnColor); editableItems->addRow(i18nc("Spot color", "Spot"), chkSpot); cmbGroups->setCurrentIndex(0); lnName->setText(i18nc("Part of a default name for a color","Color")+" " + QString::number(m_d->model->colorSet()->colorCount()+1)); lnIDName->setText(QString::number(m_d->model->colorSet()->colorCount() + 1)); bnColor->setColor(color); chkSpot->setChecked(false); if (window->exec() == KoDialog::Accepted) { QString groupName = cmbGroups->currentText(); if (groupName == defaultGroupName) { groupName = QString(); } KisSwatch newEntry; newEntry.setColor(bnColor->color()); newEntry.setName(lnName->text()); newEntry.setId(lnIDName->text()); newEntry.setSpotColor(chkSpot->isChecked()); m_d->model->addEntry(newEntry, groupName); return true; } return false; } bool KisPaletteView::removeEntryWithDialog(QModelIndex index) { bool keepColors = false; if (qvariant_cast(index.data(KisPaletteModel::IsGroupNameRole))) { QScopedPointer window(new KoDialog(this)); window->setWindowTitle(i18nc("@title:window","Removing Group")); QFormLayout *editableItems = new QFormLayout(window.data()); QCheckBox *chkKeep = new QCheckBox(window.data()); window->mainWidget()->setLayout(editableItems); editableItems->addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), chkKeep); if (window->exec() != KoDialog::Accepted) { return false; } keepColors = chkKeep->isChecked(); } m_d->model->removeEntry(index, keepColors); if (m_d->model->colorSet()->isGlobal()) { m_d->model->colorSet()->save(); } return true; } void KisPaletteView::selectClosestColor(const KoColor &color) { KoColorSet* color_set = m_d->model->colorSet(); if (!color_set) { return; } //also don't select if the color is the same as the current selection if (m_d->model->getEntry(currentIndex()).color() == color) { return; } selectionModel()->clearSelection(); QModelIndex index = m_d->model->indexForClosest(color); selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select); } void KisPaletteView::slotFGColorChanged(const KoColor &color) { selectClosestColor(color); } void KisPaletteView::mouseReleaseEvent(QMouseEvent *event) { if (selectedIndexes().size() <= 0) { return; } QModelIndex index = currentIndex(); if (qvariant_cast(index.data(KisPaletteModel::IsGroupNameRole)) == false) { emit sigIndexSelected(index); KisSwatch entry = m_d->model->getEntry(index); emit sigColorSelected(entry.color()); } QTableView::mouseReleaseEvent(event); } void KisPaletteView::setPaletteModel(KisPaletteModel *model) { if (m_d->model) { - disconnect(m_d->model, 0, this, 0); + disconnect(m_d->model, Q_NULLPTR, this, Q_NULLPTR); } m_d->model = model; setModel(model); slotAdditionalGuiUpdate(); connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), SLOT(slotAdditionalGuiUpdate())); connect(model, SIGNAL(modelReset()), SLOT(slotAdditionalGuiUpdate())); } KisPaletteModel* KisPaletteView::paletteModel() const { return m_d->model; } void KisPaletteView::setAllowModification(bool allow) { m_d->allowPaletteModification = allow; setDragEnabled(allow); setAcceptDrops(allow); } -void KisPaletteView::modifyEntry(QModelIndex index) -{ - if (!m_d->allowPaletteModification) { return; } - if (!m_d->model->colorSet()->isEditable()) { return; } - - KoDialog *dlg = new KoDialog(); - QFormLayout *editableItems = new QFormLayout(dlg); - dlg->mainWidget()->setLayout(editableItems); - QLineEdit *lnGroupName = new QLineEdit(dlg); - - if (qvariant_cast(index.data(KisPaletteModel::IsGroupNameRole))) { - //rename the group. - QString groupName = qvariant_cast(index.data(Qt::DisplayRole)); - editableItems->addRow(i18nc("Name for a colorgroup","Name"), lnGroupName); - lnGroupName->setText(groupName); - if (dlg->exec() == KoDialog::Accepted) { - if (lnGroupName->text().isEmpty()) { return; } - for (const QString &groupName: m_d->model->colorSet()->getGroupNames()) { - if (groupName == lnGroupName->text()) { - return; - } - } - m_d->model->renameGroup(groupName, lnGroupName->text()); - } - } else { - QLineEdit *lnIDName = new QLineEdit(dlg); - KisColorButton *bnColor = new KisColorButton(dlg); - QCheckBox *chkSpot = new QCheckBox(dlg); - KisSwatch entry = m_d->model->getEntry(index); - chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color.")); - editableItems->addRow(i18n("ID"), lnIDName); - editableItems->addRow(i18nc("Name for a swatch group", "Name"), lnGroupName); - editableItems->addRow(i18n("Color"), bnColor); - editableItems->addRow(i18n("Spot"), chkSpot); - lnGroupName->setText(entry.name()); - lnIDName->setText(entry.id()); - bnColor->setColor(entry.color()); - chkSpot->setChecked(entry.spotColor()); - if (dlg->exec() == KoDialog::Accepted) { - entry.setName(lnGroupName->text()); - entry.setId(lnIDName->text()); - entry.setColor(bnColor->color()); - entry.setSpotColor(chkSpot->isChecked()); - m_d->model->setEntry(entry, index); - } - } - - delete dlg; - update(index); -} - void KisPaletteView::slotHorizontalHeaderResized(int, int, int newSize) { resizeRows(newSize); slotAdditionalGuiUpdate(); } void KisPaletteView::resizeRows(int newSize) { verticalHeader()->setDefaultSectionSize(newSize); verticalHeader()->resizeSections(QHeaderView::Fixed); } void KisPaletteView::removeSelectedEntry() { if (selectedIndexes().size() <= 0) { return; } m_d->model->removeEntry(currentIndex()); } void KisPaletteView::slotAdditionalGuiUpdate() { clearSpans(); resizeRows(verticalHeader()->defaultSectionSize()); for (int groupNameRowNumber : m_d->model->m_groupNameRows.keys()) { if (groupNameRowNumber == -1) { continue; } setSpan(groupNameRowNumber, 0, 1, m_d->model->columnCount()); setRowHeight(groupNameRowNumber, fontMetrics().lineSpacing() + 6); verticalHeader()->resizeSection(groupNameRowNumber, fontMetrics().lineSpacing() + 6); } } void KisPaletteView::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer) { Q_ASSERT(m_d->model); m_d->model->setDisplayRenderer(displayRenderer); } diff --git a/libs/widgets/kis_palette_view.h b/libs/widgets/kis_palette_view.h index 9000874925..8e2ace80a6 100644 --- a/libs/widgets/kis_palette_view.h +++ b/libs/widgets/kis_palette_view.h @@ -1,115 +1,109 @@ /* * Copyright (c) 2016 Dmitry Kazakov * Copyright (c) 2017 Wolthera van Hövell tot Westerflier * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __KIS_PALETTE_VIEW_H #define __KIS_PALETTE_VIEW_H #include #include #include #include #include #include #include #include "kritawidgets_export.h" class KisPaletteModel; class QWheelEvent; class KoColorDisplayRendererInterface; class KRITAWIDGETS_EXPORT KisPaletteView : public QTableView { Q_OBJECT private: static int MININUM_ROW_HEIGHT; public: explicit KisPaletteView(QWidget *parent = Q_NULLPTR); ~KisPaletteView() override; void setPaletteModel(KisPaletteModel *model); KisPaletteModel* paletteModel() const; public: /** * @brief setAllowModification * Set whether doubleclick calls up a modification window. This is to prevent users from editing * the palette when the palette is intended to be a list of items. */ void setAllowModification(bool allow); void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer); /** * @brief setCrossedKeyword * this apparently allows you to set keywords that can cross out colors. * This is implemented to mark the lazybrush "transparent" color. * @param value */ void setCrossedKeyword(const QString &value); /** * add an entry with a dialog window. */ bool addEntryWithDialog(KoColor color); /** * remove entry with a dialog window.(Necessary for groups. */ bool removeEntryWithDialog(QModelIndex index); void removeSelectedEntry(); - /** - * @brief modifyEntry - * function for changing the entry at the given index. - * if modification isn't allow(@see setAllowModification), this does nothing. - */ - void modifyEntry(QModelIndex index); /** * @brief selectClosestColor * select a color that's closest to parameter color * @param color */ void selectClosestColor(const KoColor &color); Q_SIGNALS: void sigIndexSelected(const QModelIndex &index); void sigColorSelected(const KoColor &); public Q_SLOTS: /** * This tries to select the closest color in the palette. * This doesn't update the foreground color, just the visual selection. */ void slotFGColorChanged(const KoColor &); protected: void mouseReleaseEvent(QMouseEvent *event) override; private Q_SLOTS: void slotHorizontalHeaderResized(int, int, int newSize); void slotAdditionalGuiUpdate(); private: void resizeRows(int newSize); private: struct Private; const QScopedPointer m_d; }; #endif /* __KIS_PALETTE_VIEW_H */ diff --git a/plugins/dockers/palettedocker/CMakeLists.txt b/plugins/dockers/palettedocker/CMakeLists.txt index db0b468c32..963b119dbf 100644 --- a/plugins/dockers/palettedocker/CMakeLists.txt +++ b/plugins/dockers/palettedocker/CMakeLists.txt @@ -1,14 +1,15 @@ set(KRITA_PALETTEDOCKER_SOURCES palettedocker.cpp palettedocker_dock.cpp - PaletteListSaver.cpp - KisPaletteManager.cpp - KisChangePaletteCommand.cpp + PaletteEditor.cpp + ChangePaletteCommand.cpp + DlgPaletteEditor.cpp ) ki18n_wrap_ui(KRITA_PALETTEDOCKER_SOURCES wdgpalettedock.ui + WdgDlgPaletteEditor.ui ) add_library(kritapalettedocker MODULE ${KRITA_PALETTEDOCKER_SOURCES}) target_link_libraries(kritapalettedocker kritaui) install(TARGETS kritapalettedocker DESTINATION ${KRITA_PLUGIN_INSTALL_DIR}) diff --git a/plugins/dockers/palettedocker/ChangePaletteCommand.cpp b/plugins/dockers/palettedocker/ChangePaletteCommand.cpp new file mode 100644 index 0000000000..3c14d0aeb6 --- /dev/null +++ b/plugins/dockers/palettedocker/ChangePaletteCommand.cpp @@ -0,0 +1,20 @@ +#include +#include + +#include "ChangePaletteCommand.h" + +const char ChangePaletteCommand::MagicString[] = "Edit palette"; + +ChangePaletteCommand::ChangePaletteCommand() + : KUndo2Command(kundo2_i18n(MagicString)) +{ + +} + +ChangePaletteCommand::~ChangePaletteCommand() +{ } + +int ChangePaletteCommand::id() const +{ + return KisCommandUtils::ChangePaletteId; +} diff --git a/plugins/dockers/palettedocker/ChangePaletteCommand.h b/plugins/dockers/palettedocker/ChangePaletteCommand.h new file mode 100644 index 0000000000..19e043b1fb --- /dev/null +++ b/plugins/dockers/palettedocker/ChangePaletteCommand.h @@ -0,0 +1,27 @@ +#ifndef KISCHANGEPALETTECOMMAND_H +#define KISCHANGEPALETTECOMMAND_H + +#include + +#include +#include + +class ChangePaletteCommand : public KUndo2Command +{ +public: + static const char MagicString[]; +public: + ChangePaletteCommand(); + ~ChangePaletteCommand() override; + void undo() override + { + // palette modification shouldn't be undone; + } + void redo() override + { + // palette modification shouldn't be undone; + } + int id() const override; +}; + +#endif // KISCHANGEPALETTECOMMAND_H diff --git a/plugins/dockers/palettedocker/DlgPaletteEditor.cpp b/plugins/dockers/palettedocker/DlgPaletteEditor.cpp new file mode 100644 index 0000000000..1fef8e6456 --- /dev/null +++ b/plugins/dockers/palettedocker/DlgPaletteEditor.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "PaletteEditor.h" + +#include + +#include "DlgPaletteEditor.h" + +DlgPaletteEditor::DlgPaletteEditor() + : m_ui(new Ui_WdgDlgPaletteEditor) + , m_actAddGroup(new QAction(i18n("Add a group"))) + , m_actDelGroup(new QAction(i18n("Delete this group"))) + , m_actRenGroup(new QAction(i18n("Rename this group"))) + , m_paletteEditor(new PaletteEditor(this)) + , m_currentGroupOriginalName(KoColorSet::GLOBAL_GROUP_NAME) +{ + m_ui->setupUi(this); + m_ui->gbxPalette->setTitle(i18n("Palette settings")); + m_ui->labelFilename->setText(i18n("Filename")); + m_ui->labelName->setText(i18n("Palette Name")); + m_ui->bnAddGroup->setDefaultAction(m_actAddGroup.data()); + + m_ui->gbxGroup->setTitle(i18n("Group settings")); + m_ui->labelColCount->setText(i18n("Column count")); + m_ui->labelRowCount->setText(i18n("Row count")); + m_ui->bnDelGroup->setDefaultAction(m_actDelGroup.data()); + m_ui->bnRenGroup->setDefaultAction(m_actRenGroup.data()); + + connect(m_actAddGroup.data(), SIGNAL(triggered(bool)), SLOT(slotAddGroup())); + connect(m_actDelGroup.data(), SIGNAL(triggered(bool)), SLOT(slotDelGroup())); + connect(m_actRenGroup.data(), SIGNAL(triggered(bool)), SLOT(slotRenGroup())); + connect(m_ui->spinBoxRow, SIGNAL(valueChanged(int)), SLOT(slotRowCountChanged(int))); + connect(m_ui->spinBoxCol, SIGNAL(valueChanged(int)), SLOT(slotColCountChanged(int))); + connect(m_ui->lineEditName, SIGNAL(editingFinished()), SLOT(slotNameChanged())); + connect(m_ui->lineEditFilename, SIGNAL(textEdited(QString)), SLOT(slotFilenameChanged(QString))); + connect(m_ui->lineEditFilename, SIGNAL(editingFinished()), SLOT(slotFilenameInputFinished())); + connect(m_ui->ckxGlobal, SIGNAL(stateChanged(int)), SLOT(slotSetGlobal(int))); + connect(m_ui->ckxReadOnly, SIGNAL(stateChanged(int)), SLOT(slotSetReadOnly(int))); + + m_warnPalette.setColor(QPalette::Text, Qt::red); +} + +DlgPaletteEditor::~DlgPaletteEditor() +{ } + +void DlgPaletteEditor::setPaletteModel(KisPaletteModel *model) +{ + m_colorSet = model->colorSet(); + if (m_colorSet.isNull()) { + return; + } + m_paletteEditor->setPaletteModel(model); + + m_ui->lineEditName->setText(m_colorSet->name()); + m_ui->lineEditFilename->setText(m_colorSet->filename()); + m_ui->spinBoxCol->setValue(m_colorSet->columnCount()); + m_ui->ckxGlobal->setCheckState(m_colorSet->isGlobal() ? Qt::Checked : Qt::Unchecked); + m_ui->ckxReadOnly->setCheckState(!m_colorSet->isEditable() ? Qt::Checked : Qt::Unchecked); + + Q_FOREACH (const QString & groupName, m_colorSet->getGroupNames()) { + m_ui->cbxGroup->addItem(groupName); + } + + connect(m_ui->cbxGroup, SIGNAL(currentTextChanged(QString)), SLOT(slotGroupChosen(QString))); + m_ui->cbxGroup->setCurrentText(KoColorSet::GLOBAL_GROUP_NAME); + m_ui->bnDelGroup->setEnabled(false); + m_ui->bnRenGroup->setEnabled(false); + + m_ui->spinBoxRow->setValue(m_paletteEditor->rowNumberOfGroup(KoColorSet::GLOBAL_GROUP_NAME)); + + bool canWrite = m_colorSet->isEditable(); + m_ui->lineEditName->setEnabled(canWrite); + m_ui->lineEditFilename->setEnabled(canWrite); + m_ui->spinBoxCol->setEnabled(canWrite); + m_ui->spinBoxRow->setEnabled(canWrite); + m_ui->ckxGlobal->setEnabled(canWrite); + m_ui->ckxReadOnly->setEnabled(canWrite); + m_ui->bnAddGroup->setEnabled(canWrite); +} + +void DlgPaletteEditor::setView(KisViewManager *view) +{ + m_paletteEditor->setView(view); +} + +void DlgPaletteEditor::slotAddGroup() +{ + QString newGroupName = m_paletteEditor->addGroup(); + if (!newGroupName.isEmpty()) { + m_ui->cbxGroup->addItem(newGroupName); + m_ui->cbxGroup->setCurrentIndex(m_ui->cbxGroup->count() - 1); + }; +} + +void DlgPaletteEditor::slotRenGroup() +{ + QString newName = m_paletteEditor->renameGroup(m_currentGroupOriginalName); + if (!newName.isEmpty()) { + int idx = m_ui->cbxGroup->currentIndex(); + m_ui->cbxGroup->removeItem(idx); + m_ui->cbxGroup->insertItem(idx, newName); + m_ui->cbxGroup->setCurrentIndex(idx); + } +} + +void DlgPaletteEditor::slotDelGroup() +{ + int deletedIdx = m_ui->cbxGroup->currentIndex(); + if (m_paletteEditor->removeGroup(m_currentGroupOriginalName)) { + m_ui->cbxGroup->setCurrentIndex(0); + m_ui->cbxGroup->removeItem(deletedIdx); + } +} + +void DlgPaletteEditor::slotGroupChosen(const QString &groupName) +{ + if (groupName == KoColorSet::GLOBAL_GROUP_NAME) { + m_ui->bnDelGroup->setEnabled(false); + m_ui->bnRenGroup->setEnabled(false); + } else { + m_ui->bnDelGroup->setEnabled(true); + m_ui->bnRenGroup->setEnabled(true); + } + m_currentGroupOriginalName = m_paletteEditor->oldNameFromNewName(groupName); + m_ui->spinBoxRow->setValue(m_paletteEditor->rowNumberOfGroup(m_currentGroupOriginalName)); +} + +void DlgPaletteEditor::uploadChange() +{ + m_paletteEditor->updatePalette(); +} + +void DlgPaletteEditor::slotRowCountChanged(int newCount) +{ + m_paletteEditor->changeGroupRowCount(m_currentGroupOriginalName, newCount); +} + +void DlgPaletteEditor::slotSetGlobal(int state) +{ + m_paletteEditor->setGlobal(state == Qt::Checked); +} + +void DlgPaletteEditor::slotSetReadOnly(int state) +{ + m_paletteEditor->setReadOnly(state == Qt::Checked); +} + +void DlgPaletteEditor::slotNameChanged() +{ + m_paletteEditor->rename(qobject_cast(sender())->text()); +} + +void DlgPaletteEditor::slotFilenameChanged(const QString &newFilename) +{ + if (m_paletteEditor->duplicateExistsFilename(newFilename)) { + m_ui->lineEditFilename->setPalette(m_warnPalette); + m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); + return; + } + m_ui->lineEditFilename->setPalette(m_normalPalette); + m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); + m_paletteEditor->changeFilename(newFilename); +} + +void DlgPaletteEditor::slotFilenameInputFinished() +{ + QString newName = m_ui->lineEditFilename->text(); + if (m_paletteEditor->duplicateExistsFilename(newName)) { + return; + } + m_paletteEditor->changeFilename(newName); +} + +void DlgPaletteEditor::slotColCountChanged(int newCount) +{ + m_paletteEditor->changeColCount(newCount); +} diff --git a/plugins/dockers/palettedocker/DlgPaletteEditor.h b/plugins/dockers/palettedocker/DlgPaletteEditor.h new file mode 100644 index 0000000000..31ee897832 --- /dev/null +++ b/plugins/dockers/palettedocker/DlgPaletteEditor.h @@ -0,0 +1,68 @@ +#ifndef KISDLGPALETTEEDITOR_H +#define KISDLGPALETTEEDITOR_H + +#include +#include +#include +#include +#include +#include + +class QAction; + +class KoColorSet; +class KisPaletteModel; +class KisSwatchGroup; +class KoDialog; +class KisViewManager; + +class PaletteEditor; +class Ui_WdgDlgPaletteEditor; + +class DlgPaletteEditor : public QDialog +{ + Q_OBJECT +public: + explicit DlgPaletteEditor(); + ~DlgPaletteEditor(); + +public: + void setPaletteModel(KisPaletteModel *); + KoColorSet* palette() const { return m_colorSet; } + void setView(KisViewManager *); + + void uploadChange(); + +private Q_SLOTS: + void slotDelGroup(); + void slotAddGroup(); + void slotRenGroup(); + + void slotGroupChosen(const QString &groupName); + + void slotRowCountChanged(int); + void slotSetGlobal(int); + void slotSetReadOnly(int); + + void slotNameChanged(); + void slotFilenameChanged(const QString &newFilename); + void slotFilenameInputFinished(); + void slotColCountChanged(int); + +private: + QString oldNameFromNewName(const QString &newName) const; + +private: + QScopedPointer m_ui; + QScopedPointer m_actAddGroup; + QScopedPointer m_actDelGroup; + QScopedPointer m_actRenGroup; + QScopedPointer m_paletteEditor; + QPointer m_colorSet; + QString m_currentGroupOriginalName; + + QPalette m_normalPalette; + QPalette m_warnPalette; +}; + +#endif // KISDLGPALETTEEDITOR_H diff --git a/plugins/dockers/palettedocker/KisChangePaletteCommand.cpp b/plugins/dockers/palettedocker/KisChangePaletteCommand.cpp deleted file mode 100644 index 28b4c755e4..0000000000 --- a/plugins/dockers/palettedocker/KisChangePaletteCommand.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#include "KisChangePaletteCommand.h" - -const char KisChangePaletteCommand::MagicString[] = "Edit palette"; - -KisChangePaletteCommand::KisChangePaletteCommand() - : KUndo2Command(kundo2_i18n(MagicString)) -{ - -} - -KisChangePaletteCommand::~KisChangePaletteCommand() -{ } - -int KisChangePaletteCommand::id() const -{ - return KisCommandUtils::ChangePaletteId; -} diff --git a/plugins/dockers/palettedocker/KisChangePaletteCommand.h b/plugins/dockers/palettedocker/KisChangePaletteCommand.h deleted file mode 100644 index 75e34dde08..0000000000 --- a/plugins/dockers/palettedocker/KisChangePaletteCommand.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef KISCHANGEPALETTECOMMAND_H -#define KISCHANGEPALETTECOMMAND_H - -#include - -#include -#include - -class KisChangePaletteCommand : public KUndo2Command -{ -public: - static const char MagicString[]; -public: - KisChangePaletteCommand(); - ~KisChangePaletteCommand() override; - void undo() override { } - void redo() override { } - int id() const override; -}; - -#endif // KISCHANGEPALETTECOMMAND_H diff --git a/plugins/dockers/palettedocker/KisPaletteManager.cpp b/plugins/dockers/palettedocker/KisPaletteManager.cpp deleted file mode 100644 index 85d2f39f17..0000000000 --- a/plugins/dockers/palettedocker/KisPaletteManager.cpp +++ /dev/null @@ -1,160 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "KisPaletteManager.h" - -struct KisPaletteManager::PaletteInfo { - QString name; - QString filename; - int columnCount; - bool isGlobal; - bool isReadOnly; - QHash groups; -}; - -struct KisPaletteManager::Private -{ - bool isGlobalModified; - bool isReadOnlyModified; - bool isNameModified; - bool isFilenameModified; - bool isColumnCountModified; - QSet modifiedGroupNames; - QSet newGroupNames; - QPointer palette; - QScopedPointer modified; - QPointer view; - KoResourceServer *rServer; - KisSignalCompressor *updateSigCompressor; -}; - -KisPaletteManager::KisPaletteManager(QObject *parent) - : QObject(parent) - , m_d(new Private) -{ - m_d->palette = Q_NULLPTR; - m_d->updateSigCompressor = new KisSignalCompressor(40, KisSignalCompressor::FIRST_ACTIVE, this); - m_d->rServer = KoResourceServerProvider::instance()->paletteServer(); - connect(m_d->updateSigCompressor, SIGNAL(timeout()), SLOT(slotUpdatePalette())); -} - -KisPaletteManager::~KisPaletteManager() -{ - delete m_d->updateSigCompressor; -} - -void KisPaletteManager::setPalette(KoColorSet *palette) -{ - if (!palette) { return; } - m_d->modified->groups.clear(); - m_d->palette = palette; - m_d->modified.reset(new PaletteInfo); - m_d->modified->name = palette->name(); - m_d->modified->filename = palette->filename(); - m_d->modified->columnCount = palette->columnCount(); - m_d->modified->isGlobal = palette->isGlobal(); - m_d->modified->isReadOnly = !palette->isEditable(); - - Q_FOREACH (const QString &groupName, palette->getGroupNames()) { - KisSwatchGroup *cs = palette->getGroup(groupName); - m_d->modified->groups[groupName] = KisSwatchGroup(*cs); - } -} - -void KisPaletteManager::setView(KisViewManager *view) -{ - m_d->view = view; -} - -void KisPaletteManager::addGroup(const QString &name, int rowNumber) -{ - m_d->newGroupNames.insert(name); - m_d->modified->groups[name] = KisSwatchGroup(); - KisSwatchGroup &newGroup = m_d->modified->groups[name]; - newGroup.setName(name); - newGroup.setRowCount(rowNumber); -} - -void KisPaletteManager::removeGroup(const QString &name) -{ - m_d->modified->groups.remove(name); -} - -void KisPaletteManager::renameGroup(const QString &oldName, const QString &newName) -{ - m_d->modifiedGroupNames.insert(oldName); - m_d->modified->groups[oldName].setName(newName); -} - -void KisPaletteManager::setEntry(const KisSwatch &swatch, int col, int row, const QString &groupName) -{ - m_d->modifiedGroupNames.insert(groupName); - m_d->modified->groups[groupName].setEntry(swatch, col, row); -} - -void KisPaletteManager::removeEntry(int col, int row, const QString &groupName) -{ - m_d->modifiedGroupNames.insert(groupName); - m_d->modified->groups[groupName].removeEntry(col, row); -} - -void KisPaletteManager::addEntry(const KisSwatch &swatch, const QString &groupName) -{ - m_d->modifiedGroupNames.insert(groupName); - m_d->modified->groups[groupName].addEntry(swatch); -} - -void KisPaletteManager::slotUpdatePalette() -{ - if (!m_d->view) { return; } - if (!m_d->view->document()) { return; } - KisDocument *doc = m_d->view->document(); - KoColorSet *palette = m_d->palette; - PaletteInfo *modified = m_d->modified.data(); - - doc->addCommand(new KisChangePaletteCommand()); - - if (m_d->isGlobalModified) { - palette->setIsGlobal(modified->isGlobal); - } - if (m_d->isReadOnlyModified) { - palette->setIsEditable(palette->isEditable()); - } - if (m_d->isColumnCountModified) { - palette->setColumnCount(modified->columnCount); - } - if (m_d->isNameModified) { - palette->setName(modified->name); - } - if (m_d->isFilenameModified) { - palette->setFilename(modified->filename); - } - Q_FOREACH (const QString &groupName, palette->getGroupNames()) { - if (!modified->groups.contains(groupName)) { - palette->removeGroup(groupName); - } - } - Q_FOREACH (const QString &groupName, palette->getGroupNames()) { - KisSwatchGroup *g = palette->getGroup(groupName); - if (m_d->modifiedGroupNames.contains(groupName)) { - *g = modified->groups[groupName]; - palette->changeGroupName(groupName, modified->groups[groupName].name()); - } - } - Q_FOREACH (const QString &newGroupName, m_d->newGroupNames) { - palette->addGroup(newGroupName); - *palette->getGroup(newGroupName) = modified->groups[newGroupName]; - } -} diff --git a/plugins/dockers/palettedocker/KisPaletteManager.h b/plugins/dockers/palettedocker/KisPaletteManager.h deleted file mode 100644 index 16621b1f50..0000000000 --- a/plugins/dockers/palettedocker/KisPaletteManager.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef KISPALETTEMANAGER_H -#define KISPALETTEMANAGER_H - -#include -#include -#include - -class KoColorSet; -class KisViewManager; -class KisSwatchGroup; - -class KisPaletteManager : public QObject -{ - Q_OBJECT -public: - explicit KisPaletteManager(QObject *parent = Q_NULLPTR); - ~KisPaletteManager(); - void setPalette(KoColorSet *palette); - void setView(KisViewManager *view); - - void addGroup(const QString &name, int rowNumber); - void removeGroup(const QString &name); - void renameGroup(const QString &oldName, const QString &newName); - void setEntry(const KisSwatch &swatch, int col, int row, const QString &groupName); - void removeEntry(int col, int row, const QString &groupName); - void addEntry(const KisSwatch &swatch, const QString &groupName); - - bool isModified() const; - const KisSwatchGroup &getModifiedGroup(const QString &originalName) const; - -private Q_SLOTS: - void slotUpdatePalette(); - -private: - struct PaletteInfo; - struct Private; - QScopedPointer m_d; -}; - -#endif // KISPALETTEMANAGER_H diff --git a/plugins/dockers/palettedocker/PaletteEditor.cpp b/plugins/dockers/palettedocker/PaletteEditor.cpp new file mode 100644 index 0000000000..f3a0a8c5f6 --- /dev/null +++ b/plugins/dockers/palettedocker/PaletteEditor.cpp @@ -0,0 +1,558 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ChangePaletteCommand.h" +#include "PaletteEditor.h" + +struct PaletteEditor::PaletteInfo { + QString name; + QString filename; + int columnCount; + bool isGlobal; + bool isReadOnly; + QHash groups; +}; + +struct PaletteEditor::Private +{ + bool isGlobalModified; + bool isReadOnlyModified; + bool isNameModified; + bool isFilenameModified; + bool isColumnCountModified; + QSet modifiedGroupNames; // key is original group name + QSet newGroupNames; + QSet keepColorGroups; + QString groupBeingModified; + QPointer model; + QPointer view; + PaletteInfo modified; + QPointer query; + KoResourceServer *rServer; + + QPalette normalPalette; + QPalette warnPalette; +}; + +PaletteEditor::PaletteEditor(QObject *parent) + : QObject(parent) + , m_d(new Private) +{ + m_d->rServer = KoResourceServerProvider::instance()->paletteServer(); + m_d->warnPalette.setColor(QPalette::Text, Qt::red); +} + +PaletteEditor::~PaletteEditor() +{ } + +void PaletteEditor::setPaletteModel(KisPaletteModel *model) +{ + if (!model) { return; } + m_d->model = model; + slotPaletteChanged(); +} + +void PaletteEditor::setView(KisViewManager *view) +{ + m_d->view = view; +} + +void PaletteEditor::addPalette() +{ + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + KoResourceServerAdapter rAdapter(m_d->rServer); + KoColorSet *newColorSet = new KoColorSet(newPaletteFileName()); + newColorSet->setPaletteType(KoColorSet::KPL); + newColorSet->setIsGlobal(false); + newColorSet->setIsEditable(true); + newColorSet->setValid(true); + newColorSet->setName("New Palette"); + rAdapter.addResource(newColorSet); + + uploadPaletteList(); +} + +void PaletteEditor::importPalette() +{ + KoResourceServerAdapter rAdapter(m_d->rServer); + KoFileDialog dialog(Q_NULLPTR, KoFileDialog::OpenFile, "Open Palette"); + dialog.setDefaultDir(QDir::homePath()); + dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset" << "application/x-gimp-color-palette"); + QString filename = dialog.filename(); + if (filename.isEmpty()) { return; } + if (duplicateExistsFilename(filename)) { + QMessageBox message; + message.setWindowTitle(i18n("Can't Import Palette")); + message.setText(i18n("Can't import palette: there's already imported with the same filename")); + message.exec(); + return; + } + KoColorSet *colorSet = new KoColorSet(filename); + colorSet->load(); + rAdapter.addResource(colorSet); + + uploadPaletteList(); +} + +void PaletteEditor::removePalette(KoColorSet *cs) +{ + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + if (!cs || !cs->isEditable()) { + return; + } + if (cs->isGlobal()) { + QFile::remove(cs->filename()); + } + KoResourceServerAdapter rAdapter(m_d->rServer); + rAdapter.removeResource(cs); + + uploadPaletteList(); + m_d->view->document()->addCommand(new ChangePaletteCommand()); +} + +int PaletteEditor::rowNumberOfGroup(const QString &oriName) const +{ + if (!m_d->modified.groups.contains(oriName)) { return 0; } + return m_d->modified.groups[oriName].rowCount(); +} + +bool PaletteEditor::duplicateExistsGroupName(const QString &name) const +{ + return m_d->modified.groups.contains(name) || m_d->newGroupNames.contains(name); +} + +QString PaletteEditor::oldNameFromNewName(const QString &newName) const +{ + Q_FOREACH (const QString &oldGroupName, m_d->modified.groups.keys()) { + if (m_d->modified.groups[oldGroupName].name() == newName) { + return oldGroupName; + } + } + return QString(); +} + +void PaletteEditor::rename(const QString &newName) +{ + m_d->isNameModified = true; + m_d->modified.name = newName; +} + +void PaletteEditor::changeFilename(const QString &newName) +{ + m_d->isFilenameModified = true; + m_d->modified.filename = newName; +} + +void PaletteEditor::changeColCount(int newCount) +{ + m_d->isColumnCountModified = true; + m_d->modified.columnCount = newCount; +} + +QString PaletteEditor::addGroup() +{ + KoDialog dlg; + m_d->query = &dlg; + + QVBoxLayout layout(&dlg); + dlg.mainWidget()->setLayout(&layout); + + QLabel lblName(i18n("Name"), &dlg); + layout.addWidget(&lblName); + QLineEdit leName(&dlg); + connect(&leName, SIGNAL(textChanged(QString)), SLOT(slotGroupNameChanged(QString))); + layout.addWidget(&leName); + QLabel lblRowCount(i18n("Row count"), &dlg); + layout.addWidget(&lblRowCount); + QSpinBox spxRow(&dlg); + spxRow.setValue(20); + layout.addWidget(&spxRow); + + if (dlg.exec() != QDialog::Accepted) { return QString(); } + if (duplicateExistsGroupName(leName.text())) { return QString(); } + + QString name = leName.text(); + m_d->newGroupNames.insert(name); + m_d->modified.groups[name] = KisSwatchGroup(); + KisSwatchGroup &newGroup = m_d->modified.groups[name]; + newGroup.setName(name); + newGroup.setRowCount(spxRow.value()); + return name; +} + +bool PaletteEditor::removeGroup(const QString &name) +{ + KoDialog window; + window.setWindowTitle(i18nc("@title:window", "Removing Group")); + QFormLayout editableItems(&window); + QCheckBox chkKeep(&window); + window.mainWidget()->setLayout(&editableItems); + editableItems.addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), &chkKeep); + if (window.exec() != KoDialog::Accepted) { return false; } + + m_d->modified.groups.remove(name); + if (chkKeep.isChecked()) { + m_d->keepColorGroups.insert(name); + } + return true; +} + +QString PaletteEditor::renameGroup(const QString &oldName) +{ + if (oldName.isEmpty() || oldName == KoColorSet::GLOBAL_GROUP_NAME) { return QString(); } + + KoDialog dlg; + m_d->query = &dlg; + m_d->groupBeingModified = oldName; + + QFormLayout form(&dlg); + dlg.mainWidget()->setLayout(&form); + + QLineEdit leNewName; + connect(&leNewName, SIGNAL(textChanged(QString)), SLOT(slotGroupNameChanged(QString))); + leNewName.setText(m_d->modified.groups[oldName].name()); + + form.addRow(i18nc("Renaming swatch group", "New name"), &leNewName); + + if (dlg.exec() != KoDialog::Accepted) { return QString(); } + if (leNewName.text().isEmpty()) { return QString(); } + if (duplicateExistsGroupName(leNewName.text())) { return QString(); } + + m_d->modifiedGroupNames.insert(oldName); + m_d->modified.groups[oldName].setName(leNewName.text()); + if (m_d->newGroupNames.remove(oldName)) { + m_d->newGroupNames.insert(leNewName.text()); + } + return leNewName.text(); +} + +void PaletteEditor::slotGroupNameChanged(const QString &newName) +{ + QLineEdit *leGroupName = qobject_cast(sender()); + leGroupName->setPalette(m_d->normalPalette); + if (duplicateExistsGroupName(newName)) { + leGroupName->setPalette(m_d->warnPalette); + if (m_d->query->button(KoDialog::Ok)) { + m_d->query->button(KoDialog::Ok)->setEnabled(false); + } + return; + } + leGroupName->setPalette(m_d->normalPalette); + if (m_d->query->button(KoDialog::Ok)) { + m_d->query->button(KoDialog::Ok)->setEnabled(true); + } +} + +void PaletteEditor::changeGroupRowCount(const QString &name, int newRowCount) +{ + if (!m_d->modified.groups.contains(name)) { return; } + m_d->modified.groups[name].setRowCount(newRowCount); +} + +void PaletteEditor::setGlobal(bool isGlobal) +{ + m_d->isGlobalModified = true; + m_d->modified.isGlobal = isGlobal; +} + +void PaletteEditor::setReadOnly(bool isReadOnly) +{ + m_d->isReadOnlyModified = true; + m_d->modified.isReadOnly = isReadOnly; +} + +void PaletteEditor::setEntry(const KoColor &color, const QModelIndex &index) +{ + Q_ASSERT(m_d->model); + if (!m_d->model->colorSet()->isEditable()) { return; } + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + m_d->model->setEntry(KisSwatch(color), index); + m_d->view->document()->addCommand(new ChangePaletteCommand()); +} + +void PaletteEditor::removeEntry(const QModelIndex &index) +{ + Q_ASSERT(m_d->model); + if (!m_d->model->colorSet()->isEditable()) { return; } + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + bool keepColors = false; + if (qvariant_cast(index.data(KisPaletteModel::IsGroupNameRole))) { + removeGroup(qvariant_cast(index.data(KisPaletteModel::GroupNameRole))); + updatePalette(); + } else { + m_d->model->removeEntry(index, keepColors); + if (!m_d->model->colorSet()->isGlobal()) { + m_d->view->document()->addCommand(new ChangePaletteCommand()); + } + } + if (m_d->model->colorSet()->isGlobal()) { + m_d->model->colorSet()->save(); + return; + } +} + +void PaletteEditor::modifyEntry(const QModelIndex &index) +{ + if (!m_d->model->colorSet()->isEditable()) { return; } + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + + KoDialog dlg; + QFormLayout *editableItems = new QFormLayout(&dlg); + dlg.mainWidget()->setLayout(editableItems); + QLineEdit *lnGroupName = new QLineEdit(&dlg); + + QString groupName = qvariant_cast(index.data(Qt::DisplayRole)); + if (qvariant_cast(index.data(KisPaletteModel::IsGroupNameRole))) { + renameGroup(groupName); + } else { + QLineEdit *lnIDName = new QLineEdit(&dlg); + KisColorButton *bnColor = new KisColorButton(&dlg); + QCheckBox *chkSpot = new QCheckBox(&dlg); + KisSwatch entry = m_d->model->getEntry(index); + chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color.")); + editableItems->addRow(i18n("ID"), lnIDName); + editableItems->addRow(i18nc("Name for a swatch group", "Name"), lnGroupName); + editableItems->addRow(i18n("Color"), bnColor); + editableItems->addRow(i18n("Spot"), chkSpot); + lnGroupName->setText(entry.name()); + lnIDName->setText(entry.id()); + bnColor->setColor(entry.color()); + chkSpot->setChecked(entry.spotColor()); + if (dlg.exec() == KoDialog::Accepted) { + entry.setName(lnGroupName->text()); + entry.setId(lnIDName->text()); + entry.setColor(bnColor->color()); + entry.setSpotColor(chkSpot->isChecked()); + m_d->model->setEntry(entry, index); + m_d->view->document()->addCommand(new ChangePaletteCommand()); + } + } +} + +void PaletteEditor::addEntry(const KoColor &color) +{ + Q_ASSERT(m_d->model); + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + if (!m_d->model->colorSet()->isEditable()) { return; } + QScopedPointer window(new KoDialog); + window->setWindowTitle(i18nc("@title:window", "Add a new Colorset Entry")); + QFormLayout *editableItems = new QFormLayout(window.data()); + window->mainWidget()->setLayout(editableItems); + QComboBox *cmbGroups = new QComboBox(window.data()); + cmbGroups->addItems(m_d->model->colorSet()->getGroupNames()); + QLineEdit *lnIDName = new QLineEdit(window.data()); + QLineEdit *lnName = new QLineEdit(window.data()); + KisColorButton *bnColor = new KisColorButton(window.data()); + QCheckBox *chkSpot = new QCheckBox(window.data()); + chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color.")); + editableItems->addRow(i18n("Group"), cmbGroups); + editableItems->addRow(i18n("ID"), lnIDName); + editableItems->addRow(i18n("Name"), lnName); + editableItems->addRow(i18n("Color"), bnColor); + editableItems->addRow(i18nc("Spot color", "Spot"), chkSpot); + cmbGroups->setCurrentIndex(0); + lnName->setText(i18nc("Part of a default name for a color","Color") + + " " + + QString::number(m_d->model->colorSet()->colorCount()+1)); + lnIDName->setText(QString::number(m_d->model->colorSet()->colorCount() + 1)); + bnColor->setColor(color); + chkSpot->setChecked(false); + + if (window->exec() != KoDialog::Accepted) { return; } + + QString groupName = cmbGroups->currentText(); + + KisSwatch newEntry; + newEntry.setColor(bnColor->color()); + newEntry.setName(lnName->text()); + newEntry.setId(lnIDName->text()); + newEntry.setSpotColor(chkSpot->isChecked()); + m_d->model->addEntry(newEntry, groupName); + + if (m_d->model->colorSet()->isGlobal()) { + m_d->model->colorSet()->save(); + return; + } + m_d->modifiedGroupNames.insert(groupName); + m_d->modified.groups[groupName].addEntry(newEntry); + m_d->view->document()->addCommand(new ChangePaletteCommand()); +} + +void PaletteEditor::updatePalette() +{ + Q_ASSERT(m_d->model); + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + KoColorSet *palette = m_d->model->colorSet(); + PaletteInfo &modified = m_d->modified; + + if (m_d->isGlobalModified) { + palette->setIsGlobal(modified.isGlobal); + if (modified.isGlobal) { + setGlobal(); + } else { + setNonGlobal(); + } + } + if (m_d->isReadOnlyModified) { + palette->setIsEditable(palette->isEditable()); + } + if (m_d->isColumnCountModified) { + palette->setColumnCount(modified.columnCount); + } + if (m_d->isNameModified) { + palette->setName(modified.name); + } + if (m_d->isFilenameModified) { + palette->setFilename(modified.filename); + } + Q_FOREACH (const QString &groupName, palette->getGroupNames()) { + if (!modified.groups.contains(groupName)) { + m_d->model->removeGroup(groupName, m_d->keepColorGroups.contains(groupName)); + } + } + Q_FOREACH (const QString &groupName, palette->getGroupNames()) { + KisSwatchGroup *g = palette->getGroup(groupName); + if (m_d->modifiedGroupNames.contains(groupName)) { + *g = modified.groups[groupName]; + m_d->model->renameGroup(groupName, modified.groups[groupName].name()); + } + } + Q_FOREACH (const QString &newGroupName, m_d->newGroupNames) { + m_d->model->addGroup(modified.groups[newGroupName]); + } + + if (!palette->isGlobal()) { + m_d->view->document()->addCommand(new ChangePaletteCommand); + } +} + +void PaletteEditor::slotPaletteChanged() +{ + Q_ASSERT(m_d->model); + if (!m_d->model->colorSet()) { return; } + KoColorSet *palette = m_d->model->colorSet(); + m_d->modified.groups.clear(); + m_d->keepColorGroups.clear(); + m_d->newGroupNames.clear(); + m_d->modifiedGroupNames.clear(); + + m_d->modified.name = palette->name(); + m_d->modified.filename = palette->filename(); + m_d->modified.columnCount = palette->columnCount(); + m_d->modified.isGlobal = palette->isGlobal(); + m_d->modified.isReadOnly = !palette->isEditable(); + + Q_FOREACH (const QString &groupName, palette->getGroupNames()) { + KisSwatchGroup *cs = palette->getGroup(groupName); + m_d->modified.groups[groupName] = KisSwatchGroup(*cs); + } +} + +void PaletteEditor::setGlobal() +{ + Q_ASSERT(m_d->model); + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + if (!m_d->model->colorSet()) { return; } + + KoColorSet *colorSet = m_d->model->colorSet(); + QString saveLocation = m_d->rServer->saveLocation(); + QString name = colorSet->filename(); + + QFileInfo fileInfo(saveLocation + name); + + colorSet->setFilename(fileInfo.filePath()); + colorSet->setIsGlobal(true); + + uploadPaletteList(); + m_d->view->document()->addCommand(new ChangePaletteCommand()); +} + +bool PaletteEditor::duplicateExistsFilename(const QString &name) const +{ + Q_FOREACH (const KoResource *r, KoResourceServerProvider::instance()->paletteServer()->resources()) { + if (r->filename() == name && r != m_d->model->colorSet()) { + return true; + } + } + return false; +} + +void PaletteEditor::setNonGlobal() +{ + Q_ASSERT(m_d->model); + if (!m_d->view) { return; } + if (!m_d->view->document()) { return; } + if (!m_d->model->colorSet()) { return; } + QString filename = newPaletteFileName(); + KoColorSet *colorSet = m_d->model->colorSet(); + QFile::remove(colorSet->filename()); + colorSet->setFilename(filename); + colorSet->setIsGlobal(false); + + uploadPaletteList(); +} + +QString PaletteEditor::newPaletteFileName() +{ + QSet nameSet; + + Q_FOREACH (const KoResource *r, m_d->rServer->resources()) { + nameSet.insert(r->filename()); + } + + KoColorSet tmpColorSet; + QString result = "new_palette_"; + + int i = 0; + while (nameSet.contains(result + QString::number(i) + tmpColorSet.defaultFileExtension())) { + i++; + } + result = result + QString::number(i) + tmpColorSet.defaultFileExtension(); + return result; +} + +void PaletteEditor::uploadPaletteList() const +{ + QList list; + Q_FOREACH (KoResource * paletteResource, m_d->rServer->resources()) { + KoColorSet *palette = static_cast(paletteResource); + Q_ASSERT(palette); + if (!palette->isGlobal()) { + list.append(palette); + } + } + m_d->view->document()->setPaletteList(list); + m_d->view->document()->addCommand(new ChangePaletteCommand()); +} diff --git a/plugins/dockers/palettedocker/PaletteEditor.h b/plugins/dockers/palettedocker/PaletteEditor.h new file mode 100644 index 0000000000..f9377f499a --- /dev/null +++ b/plugins/dockers/palettedocker/PaletteEditor.h @@ -0,0 +1,88 @@ +#ifndef KISPALETTEMANAGER_H +#define KISPALETTEMANAGER_H + +#include +#include +#include + +class KoColorSet; +class KisPaletteModel; +class KisViewManager; +class KisSwatchGroup; +class KisViewManager; + +class PaletteEditor : public QObject +{ + Q_OBJECT +public: + struct PaletteInfo; + +public: + explicit PaletteEditor(QObject *parent = Q_NULLPTR); + ~PaletteEditor(); + + void setPaletteModel(KisPaletteModel *model); + void setView(KisViewManager *view); + + void addPalette(); + void importPalette(); + void removePalette(KoColorSet *); + + int rowNumberOfGroup(const QString &oriName) const; + QString oldNameFromNewName(const QString &newName) const; + bool duplicateExistsFilename(const QString &name) const; + + void rename(const QString &newName); + void changeFilename(const QString &newName); + void changeColCount(int); + + /** + * @brief addGroup + * @param name original group name + * @param rowNumber + * @return new group's name if change accpeted, empty string if cancelled + */ + QString addGroup(); + /** + * @brief removeGroup + * @param name original group name + * @return true if change accepted, false if cancelled + */ + bool removeGroup(const QString &name); + /** + * @brief renameGroup + * @param oldName + * @return new name if change accpeted, empty string if cancelled + */ + QString renameGroup(const QString &oldName); + void changeGroupRowCount(const QString &name, int newRowCount); + void setGlobal(bool); + void setReadOnly(bool); + + void setEntry(const KoColor &color, const QModelIndex &index); + void removeEntry(const QModelIndex &index); + void modifyEntry(const QModelIndex &index); + void addEntry(const KoColor &color); + + bool isModified() const; + const KisSwatchGroup &getModifiedGroup(const QString &originalName) const; + + void updatePalette(); + +private Q_SLOTS: + void slotGroupNameChanged(const QString &newName); + void slotPaletteChanged(); + +private: + QString newPaletteFileName(); + void setNonGlobal(); + void setGlobal(); + bool duplicateExistsGroupName(const QString &name) const; + void uploadPaletteList() const; + +private: + struct Private; + QScopedPointer m_d; +}; + +#endif // KISPALETTEMANAGER_H diff --git a/plugins/dockers/palettedocker/PaletteListSaver.cpp b/plugins/dockers/palettedocker/PaletteListSaver.cpp deleted file mode 100644 index 43023da6c8..0000000000 --- a/plugins/dockers/palettedocker/PaletteListSaver.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "PaletteListSaver.h" - -#include "palettedocker_dock.h" - -#include -#include -#include -#include -#include -#include - -PaletteListSaver::PaletteListSaver(PaletteDockerDock *dockerDock, QObject *parent) - : QObject(parent) - , m_dockerDock(dockerDock) -{ - connect(m_dockerDock->m_model, SIGNAL(sigPaletteModifed()), - SLOT(slotPaletteModified())); - connect(m_dockerDock->m_paletteChooser, SIGNAL(sigPaletteListChanged()), - SLOT(slotSetPaletteList())); -} - -void PaletteListSaver::slotSetPaletteList() -{ - QList list; - for (KoResource * paletteResource : - KoResourceServerProvider::instance()->paletteServer()->resources()) { - KoColorSet *palette = static_cast(paletteResource); - Q_ASSERT(palette); - if (!palette->isGlobal()) { - list.append(palette); - } - } - m_dockerDock->m_view->document()->setPaletteList(list); - slotPaletteModified(); -} - -void PaletteListSaver::slotPaletteModified() -{ - resetConnection(); - KisDocument *doc = m_dockerDock->m_view->document(); - QList list; - for (const KoResource * paletteResource : - KoResourceServerProvider::instance()->paletteServer()->resources()) { - const KoColorSet *palette = static_cast(paletteResource); - Q_ASSERT(palette); - if (!palette->isGlobal()) { - list.append(KoColorSet(*palette)); - } - } - doc->setModified(true); -} - -void PaletteListSaver::resetConnection() -{ - m_dockerDock->m_view->document()->disconnect(this); - connect(m_dockerDock->m_view->document(), SIGNAL(sigSavingFinished()), - SLOT(slotSavingFinished())); -} - -void PaletteListSaver::slotSavingFinished() -{ - bool undoStackClean = m_dockerDock->m_view->document()->undoStack()->isClean(); - m_dockerDock->m_view->document()->setModified(!undoStackClean); -} diff --git a/plugins/dockers/palettedocker/PaletteListSaver.h b/plugins/dockers/palettedocker/PaletteListSaver.h deleted file mode 100644 index d4fc32634d..0000000000 --- a/plugins/dockers/palettedocker/PaletteListSaver.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef PALETTELISTSAVER_H -#define PALETTELISTSAVER_H - -#include - -class PaletteDockerDock; -class KoColorSet; - -class PaletteListSaver : public QObject -{ - Q_OBJECT -public: - PaletteListSaver(PaletteDockerDock *dockerDock, QObject *parent = Q_NULLPTR); - -private Q_SLOTS: - void slotSetPaletteList(); - void slotSavingFinished(); - void slotPaletteModified(); - -private: - /** - * @brief resetConnection - * KisDocument won't reset modified to false after saving, so I need to - * use the connection to trigger slotSavingFinished to manually set - * modified to false - */ - void resetConnection(); - -private: - friend class PaletteDockerDock; - -private: - PaletteDockerDock *m_dockerDock; -}; - -#endif // PALETTELISTSAVER_H diff --git a/libs/widgets/WdgDlgPaletteEditor.ui b/plugins/dockers/palettedocker/WdgDlgPaletteEditor.ui similarity index 99% rename from libs/widgets/WdgDlgPaletteEditor.ui rename to plugins/dockers/palettedocker/WdgDlgPaletteEditor.ui index 0986510f00..0b3fbd8064 100644 --- a/libs/widgets/WdgDlgPaletteEditor.ui +++ b/plugins/dockers/palettedocker/WdgDlgPaletteEditor.ui @@ -1,214 +1,214 @@ WdgDlgPaletteEditor 0 0 524 - 479 + 481 Dialog Palette settings Palette Name File name Column count Global Read only Qt::Vertical 20 40 0 0 Add a group Group settings Row count Qt::Vertical 20 40 0 0 Rename this group 0 0 Delete this group Qt::Horizontal QDialogButtonBox::Cancel|QDialogButtonBox::Ok buttonBox rejected() WdgDlgPaletteEditor reject() 316 260 286 274 buttonBox accepted() WdgDlgPaletteEditor accept() 248 254 157 274 diff --git a/plugins/dockers/palettedocker/palettedocker_dock.cpp b/plugins/dockers/palettedocker/palettedocker_dock.cpp index 8a4060c45c..57ae5e9cf4 100644 --- a/plugins/dockers/palettedocker/palettedocker_dock.cpp +++ b/plugins/dockers/palettedocker/palettedocker_dock.cpp @@ -1,336 +1,363 @@ /* * Copyright (c) 2013 Sven Langkamp * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "palettedocker_dock.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include "KisPaletteModel.h" -#include "KisPaletteDelegate.h" -#include "kis_palette_view.h" +#include +#include +#include #include +#include "PaletteEditor.h" +#include "DlgPaletteEditor.h" + #include "ui_wdgpalettedock.h" PaletteDockerDock::PaletteDockerDock( ) : QDockWidget(i18n("Palette")) , m_ui(new Ui_WdgPaletteDock()) , m_model(new KisPaletteModel(this)) , m_paletteChooser(new KisPaletteListWidget(this)) , m_view(Q_NULLPTR) , m_resourceProvider(Q_NULLPTR) , m_rServer(KoResourceServerProvider::instance()->paletteServer()) , m_rAdapter(new KoResourceServerAdapter(KoResourceServerProvider::instance()->paletteServer())) , m_activeDocument(Q_NULLPTR) - , m_saver(new PaletteListSaver(this)) + , m_paletteEditor(new PaletteEditor) , m_actAdd(new QAction(KisIconUtils::loadIcon("list-add"), i18n("Add foreground color"))) , m_actRemove(new QAction(KisIconUtils::loadIcon("edit-delete"), i18n("Delete color"))) , m_actModify(new QAction(KisIconUtils::loadIcon("edit-rename"), i18n("Modify this spot"))) + , m_actEditPalette(new QAction(KisIconUtils::loadIcon("groupLayer"), i18n("Edit this palette"))) { QWidget *mainWidget = new QWidget(this); setWidget(mainWidget); m_ui->setupUi(mainWidget); m_ui->bnAdd->setDefaultAction(m_actAdd.data()); m_ui->bnRemove->setDefaultAction(m_actRemove.data()); m_ui->bnRename->setDefaultAction(m_actModify.data()); + m_ui->bnEditPalette->setDefaultAction(m_actEditPalette.data()); // to make sure their icons have the same size m_ui->bnRemove->setIconSize(QSize(16, 16)); m_ui->bnRename->setIconSize(QSize(16, 16)); m_ui->bnAdd->setIconSize(QSize(16, 16)); - // m_wdgPaletteDock->bnAddGroup->setIcon(KisIconUtils::loadIcon("groupLayer")); + m_ui->bnEditPalette->setIconSize(QSize(16, 16)); m_ui->paletteView->setPaletteModel(m_model); m_ui->paletteView->setAllowModification(true); m_ui->cmbNameList->setPaletteModel(m_model); + m_paletteEditor->setPaletteModel(m_model); + connect(m_actAdd.data(), SIGNAL(triggered()), SLOT(slotAddColor())); connect(m_actRemove.data(), SIGNAL(triggered()), SLOT(slotRemoveColor())); connect(m_actModify.data(), SIGNAL(triggered()), SLOT(slotEditEntry())); + connect(m_actEditPalette.data(), SIGNAL(triggered()), SLOT(slotEditPalette())); connect(m_ui->paletteView, SIGNAL(sigIndexSelected(QModelIndex)), SLOT(slotPaletteIndexSelected(QModelIndex))); connect(m_ui->paletteView, SIGNAL(doubleClicked(QModelIndex)), SLOT(slotPaletteIndexDoubleClicked(QModelIndex))); connect(m_ui->paletteView, SIGNAL(sigIndexSelected(QModelIndex)), m_ui->cmbNameList, SLOT(slotSwatchSelected(QModelIndex))); connect(m_ui->cmbNameList, SIGNAL(sigColorSelected(KoColor)), SLOT(slotNameListSelection(KoColor))); m_viewContextMenu.addAction(m_actRemove.data()); m_viewContextMenu.addAction(m_actModify.data()); m_paletteChooser->setAllowModification(true); connect(m_paletteChooser, SIGNAL(sigPaletteSelected(KoColorSet*)), SLOT(slotSetColorSet(KoColorSet*))); + connect(m_paletteChooser, SIGNAL(sigAddPalette()), SLOT(slotAddPalette())); + connect(m_paletteChooser, SIGNAL(sigImportPalette()), SLOT(slotImportPalette())); + connect(m_paletteChooser, SIGNAL(sigRemovePalette(KoColorSet*)), SLOT(slotRemovePalette(KoColorSet*))); + connect(m_paletteChooser, SIGNAL(sigExportPalette(KoColorSet*)), SLOT(slotExportPalette(KoColorSet*))); m_ui->bnColorSets->setIcon(KisIconUtils::loadIcon("hi16-palette_library")); m_ui->bnColorSets->setToolTip(i18n("Choose palette")); m_ui->bnColorSets->setPopupWidget(m_paletteChooser); KisConfig cfg(true); QString defaultPaletteName = cfg.defaultPalette(); KoColorSet* defaultPalette = m_rServer->resourceByName(defaultPaletteName); if (defaultPalette) { slotSetColorSet(defaultPalette); } else { m_ui->bnAdd->setEnabled(false); m_ui->bnRename->setEnabled(false); m_ui->bnRemove->setEnabled(false); + m_ui->bnEditPalette->setEnabled(false); m_ui->paletteView->setAllowModification(false); } } PaletteDockerDock::~PaletteDockerDock() { } void PaletteDockerDock::setViewManager(KisViewManager* kisview) { m_view = kisview; m_resourceProvider = kisview->resourceProvider(); connect(m_resourceProvider, SIGNAL(sigSavingWorkspace(KisWorkspaceResource*)), SLOT(saveToWorkspace(KisWorkspaceResource*))); connect(m_resourceProvider, SIGNAL(sigLoadingWorkspace(KisWorkspaceResource*)), SLOT(loadFromWorkspace(KisWorkspaceResource*))); connect(m_resourceProvider, SIGNAL(sigFGColorChanged(KoColor)), m_ui->paletteView, SLOT(slotFGColorChanged(KoColor))); kisview->nodeManager()->disconnect(m_model); } +void PaletteDockerDock::slotAddPalette() +{ + m_paletteEditor->addPalette(); +} + +void PaletteDockerDock::slotRemovePalette(KoColorSet *cs) +{ + m_paletteEditor->removePalette(cs); +} + +void PaletteDockerDock::slotImportPalette() +{ + m_paletteEditor->importPalette(); +} + +void PaletteDockerDock::slotExportPalette(KoColorSet *palette) +{ + KoFileDialog dialog(this, KoFileDialog::SaveFile, "Save Palette"); + dialog.setDefaultDir(palette->filename()); + dialog.setMimeTypeFilters(QStringList() << "krita/x-colorset"); + QString newPath; + bool isStandAlone = palette->isGlobal(); + QString oriPath = palette->filename(); + if ((newPath = dialog.filename()).isEmpty()) { return; } + palette->setFilename(newPath); + palette->setIsGlobal(true); + palette->save(); + palette->setFilename(oriPath); + palette->setIsGlobal(isStandAlone); +} + void PaletteDockerDock::setCanvas(KoCanvasBase *canvas) { setEnabled(canvas != Q_NULLPTR); if (canvas) { KisCanvas2 *cv = qobject_cast(canvas); m_ui->paletteView->setDisplayRenderer(cv->displayColorConverter()->displayRendererInterface()); } if (m_activeDocument) { for (KoColorSet * &cs : m_activeDocument->paletteList()) { KoColorSet *tmpAddr = cs; cs = new KoColorSet(*cs); m_rAdapter->removeResource(tmpAddr); } } if (m_view && m_view->document()) { m_activeDocument = m_view->document(); + m_paletteEditor->setView(m_view); for (KoColorSet *cs : m_activeDocument->paletteList()) { m_rAdapter->addResource(cs); } } + if (!m_currentColorSet) { slotSetColorSet(Q_NULLPTR); } } void PaletteDockerDock::unsetCanvas() { setEnabled(false); m_ui->paletteView->setDisplayRenderer(Q_NULLPTR); + m_paletteEditor->setView(Q_NULLPTR); + for (KoResource *r : m_rServer->resources()) { KoColorSet *g = static_cast(r); if (!g->isGlobal()) { m_rAdapter->removeResource(r); } } if (!m_currentColorSet) { slotSetColorSet(Q_NULLPTR); } } void PaletteDockerDock::slotSetColorSet(KoColorSet* colorSet) { if (colorSet && colorSet->isEditable()) { m_ui->bnAdd->setEnabled(true); m_ui->bnRename->setEnabled(true); m_ui->bnRemove->setEnabled(true); + m_ui->bnEditPalette->setEnabled(true); m_ui->paletteView->setAllowModification(true); } else { m_ui->bnAdd->setEnabled(false); m_ui->bnRename->setEnabled(false); m_ui->bnRemove->setEnabled(false); + m_ui->bnEditPalette->setEnabled(false); m_ui->paletteView->setAllowModification(false); } m_currentColorSet = colorSet; m_model->setColorSet(colorSet); if (colorSet) { KisConfig cfg(true); cfg.setDefaultPalette(colorSet->name()); m_ui->bnColorSets->setText(colorSet->name()); } else { m_ui->bnColorSets->setText(""); } } -void PaletteDockerDock::slotViewChanged() -{ - // for some reason rAdapter's resources gives only an empty list - KoResourceServer* rServer = KoResourceServerProvider::instance()->paletteServer(); - if (m_activeDocument) { - for (KoColorSet * &cs : m_activeDocument->paletteList()) { - KoColorSet *tmpAddr = cs; - cs = new KoColorSet(*cs); - m_rAdapter->removeResource(tmpAddr); - } - } else { // all files was closed - for (KoResource *r : rServer->resources()) { - KoColorSet *g = static_cast(r); - if (!g->isGlobal()) { - m_rAdapter->removeResource(r); - } - } - } - if (m_view && m_view->document()) { - m_activeDocument = m_view->document(); - - for (KoColorSet *cs : m_activeDocument->paletteList()) { - m_rAdapter->addResource(cs); - } - } - if (!m_currentColorSet || (m_currentColorSet && !m_currentColorSet->isGlobal())) { - slotSetColorSet(Q_NULLPTR); - } -} - -void PaletteDockerDock::slotDocRemoved(const QString &objName) +void PaletteDockerDock::slotEditPalette() { - qDebug() << objName << "is removed"; + DlgPaletteEditor dlg; + if (!m_currentColorSet) { return; } + dlg.setPaletteModel(m_model); + dlg.setView(m_view); + if (dlg.exec() != QDialog::Accepted){ return; } + dlg.uploadChange(); + + slotSetColorSet(m_currentColorSet); } void PaletteDockerDock::slotAddColor() { if (m_currentColorSet->isEditable()) { if (m_resourceProvider) { - m_ui->paletteView->addEntryWithDialog(m_resourceProvider->fgColor()); + m_paletteEditor->addEntry(m_resourceProvider->fgColor()); } } } void PaletteDockerDock::slotRemoveColor() { if (m_currentColorSet->isEditable()) { QModelIndex index = m_ui->paletteView->currentIndex(); if (!index.isValid()) { return; } m_ui->paletteView->removeEntryWithDialog(index); m_ui->bnRemove->setEnabled(false); } } void PaletteDockerDock::setFGColorByPalette(const KisSwatch &entry) { if (m_resourceProvider) { m_resourceProvider->setFGColor(entry.color()); } } void PaletteDockerDock::saveToWorkspace(KisWorkspaceResource* workspace) { if (!m_currentColorSet.isNull()) { workspace->setProperty("palette", m_currentColorSet->name()); } } void PaletteDockerDock::loadFromWorkspace(KisWorkspaceResource* workspace) { if (workspace->hasProperty("palette")) { KoResourceServer* rServer = KoResourceServerProvider::instance()->paletteServer(); KoColorSet* colorSet = rServer->resourceByName(workspace->getString("palette")); if (colorSet) { slotSetColorSet(colorSet); } } } void PaletteDockerDock::slotPaletteIndexSelected(const QModelIndex &index) { bool slotEmpty = !(qvariant_cast(index.data(KisPaletteModel::CheckSlotRole))); if (slotEmpty) { if (!m_currentColorSet->isEditable()) { return; } setEntryByForeground(index); } else { m_ui->bnRemove->setEnabled(true); KisSwatch entry = m_model->getEntry(index); setFGColorByPalette(entry); } } void PaletteDockerDock::slotPaletteIndexDoubleClicked(const QModelIndex &index) { - m_ui->paletteView->modifyEntry(index); + m_paletteEditor->modifyEntry(index); } void PaletteDockerDock::setEntryByForeground(const QModelIndex &index) { - m_model->setEntry(KisSwatch(m_resourceProvider->fgColor()), index); + m_paletteEditor->setEntry(m_resourceProvider->fgColor(), index); if (m_currentColorSet->isEditable()) { m_ui->bnRemove->setEnabled(true); } } void PaletteDockerDock::slotEditEntry() { if (m_currentColorSet->isEditable()) { QModelIndex index = m_ui->paletteView->currentIndex(); if (!index.isValid()) { return; } - m_ui->paletteView->modifyEntry(index); + m_paletteEditor->modifyEntry(index); } } void PaletteDockerDock::slotNameListSelection(const KoColor &color) { m_resourceProvider->setFGColor(color); } diff --git a/plugins/dockers/palettedocker/palettedocker_dock.h b/plugins/dockers/palettedocker/palettedocker_dock.h index 2b2d1e03e0..d76da14cf0 100644 --- a/plugins/dockers/palettedocker/palettedocker_dock.h +++ b/plugins/dockers/palettedocker/palettedocker_dock.h @@ -1,118 +1,110 @@ /* * Copyright (c) 2013 Sven Langkamp * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef PALETTEDOCKER_DOCK_H #define PALETTEDOCKER_DOCK_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include "PaletteListSaver.h" - class KisViewManager; class KisCanvasResourceProvider; class KisWorkspaceResource; class KisPaletteListWidget; class KisPaletteModel; + +class PaletteEditor; class Ui_WdgPaletteDock; class PaletteDockerDock : public QDockWidget, public KisMainwindowObserver { Q_OBJECT public: PaletteDockerDock(); ~PaletteDockerDock() override; public: // QDockWidget void setCanvas(KoCanvasBase *canvas) override; void unsetCanvas() override; public: // KisMainWindowObserver void setViewManager(KisViewManager* kisview) override; private Q_SLOTS: + void slotAddPalette(); + void slotRemovePalette(KoColorSet *); + void slotImportPalette(); + void slotExportPalette(KoColorSet *); + void slotAddColor(); void slotRemoveColor(); void slotEditEntry(); + void slotEditPalette(); void slotPaletteIndexSelected(const QModelIndex &index); void slotPaletteIndexDoubleClicked(const QModelIndex &index); void slotNameListSelection(const KoColor &color); void slotSetColorSet(KoColorSet* colorSet); - void slotViewChanged(); - void slotDocRemoved(const QString &objName); void saveToWorkspace(KisWorkspaceResource* workspace); void loadFromWorkspace(KisWorkspaceResource* workspace); private: void setEntryByForeground(const QModelIndex &index); void setFGColorByPalette(const KisSwatch &entry); -private /* friends */: - /** - * @brief PaletteListSaver - * saves non-global palette list to KisDocument. - * Actually, this should be implemented in - * KisPaletteListWidget, but that class is in the - * library kritawidgets, while KisDocument is in - * kritaui, which depends on kritawidgets... - * - * Hope one day kritaui can finally be cleaned up... - */ - friend class PaletteListSaver; - private /* member variables */: QScopedPointer m_ui; KisPaletteModel *m_model; KisPaletteListWidget *m_paletteChooser; QPointer m_view; KisCanvasResourceProvider *m_resourceProvider; KoResourceServer * const m_rServer; QScopedPointer > m_rAdapter; QPointer m_activeDocument; QPointer m_currentColorSet; - QScopedPointer m_saver; + QScopedPointer m_paletteEditor; QScopedPointer m_actAdd; QScopedPointer m_actRemove; QScopedPointer m_actModify; + QScopedPointer m_actEditPalette; QMenu m_viewContextMenu; }; #endif diff --git a/plugins/dockers/palettedocker/wdgpalettedock.ui b/plugins/dockers/palettedocker/wdgpalettedock.ui index 6a679adf05..f937f1fcd1 100644 --- a/plugins/dockers/palettedocker/wdgpalettedock.ui +++ b/plugins/dockers/palettedocker/wdgpalettedock.ui @@ -1,151 +1,161 @@ WdgPaletteDock 0 0 647 422 1 0 0 0 0 0 16 true - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - + + - Delete color + Add foreground color ... - + .. 22 22 false - - + + - Add foreground color + Delete color ... - + .. 22 22 false + + + + + + + + .. + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + - - + + - + ... - - .. + KisPopupButton QPushButton
kis_popup_button.h
KisPaletteView QTableView
kis_palette_view.h
KisPaletteComboBox QComboBox
KisPaletteComboBox.h
bnRemove