17#include "colorpalettewidget.h"
18#include "ui_colorpalette.h"
25#include <QListWidgetItem>
26#include <QInputDialog>
27#include <QColorDialog>
34#include <QAbstractItemModel>
41#include "layerbitmap.h"
42#include "colormanager.h"
45ColorPaletteWidget::ColorPaletteWidget(
QWidget* parent) :
47 ui(new Ui::ColorPalette)
52ColorPaletteWidget::~ColorPaletteWidget()
57void ColorPaletteWidget::initUI()
60 int colorGridSize = settings.value(
"PreferredColorGridSize", 34).toInt();
61 mFitSwatches = settings.value(
"FitSwatchSize",
false).toBool();
67 mIconSize =
QSize(colorGridSize, colorGridSize);
71 QString sViewMode = settings.value(
"ColorPaletteViewMode",
"ListMode").toString();
72 if (sViewMode ==
"ListMode")
77 buttonStylesheet =
"::menu-indicator{ image: none; }"
78 "QPushButton { border: 0px; }"
79 "QPushButton:pressed { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }"
80 "QPushButton:checked { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
82 ui->addColorButton->setStyleSheet(buttonStylesheet);
83 ui->removeColorButton->setStyleSheet(buttonStylesheet);
84 ui->colorDialogButton->setStyleSheet(buttonStylesheet);
99 connect(editor(), &Editor::objectLoaded,
this, &ColorPaletteWidget::updateUI);
102void ColorPaletteWidget::updateUI()
104 mObject = mEditor->object();
109void ColorPaletteWidget::setCore(
Editor* editor)
112 mObject = mEditor->object();
115void ColorPaletteWidget::showContextMenu(
const QPoint& pos)
117 QPoint globalPos = ui->colorListWidget->mapToGlobal(
pos);
122 menu->
addAction(
tr(
"Add"),
this, &ColorPaletteWidget::addItem, 0);
123 menu->
addAction(
tr(
"Replace"),
this, &ColorPaletteWidget::replaceItem, 0);
124 menu->
addAction(
tr(
"Remove"),
this, &ColorPaletteWidget::removeItem, 0);
126 menu->
exec(globalPos);
129void ColorPaletteWidget::addItem()
136 const int colorIndex = ui->colorListWidget->count();
137 mObject->addColorAtIndex(colorIndex, ref);
147 ui->colorListWidget->editItem(item);
148 ui->colorListWidget->scrollToItem(item);
151void ColorPaletteWidget::replaceItem()
154 int index = ui->colorListWidget->currentRow();
158 if (index < 0 ) {
return; }
160 updateItemColor(index, newColor);
161 emit colorNumberChanged(index);
162 ui->colorListWidget->setCurrentRow(index);
165void ColorPaletteWidget::removeItem()
168 clickRemoveColorButton();
171void ColorPaletteWidget::selectColorNumber(
int colorNumber)
const
173 ui->colorListWidget->setCurrentRow(colorNumber);
176int ColorPaletteWidget::currentColorNumber()
178 if (ui->colorListWidget->currentRow() < 0)
180 ui->colorListWidget->setCurrentRow(0);
182 return ui->colorListWidget->currentRow();
185void ColorPaletteWidget::refreshColorList()
188 if (ui->colorListWidget->count() > 0)
190 ui->colorListWidget->clear();
193 int colorCount = editor()->object()->getColorCount();
200 selectColorNumber(editor()->color()->frontColorNumber());
205void ColorPaletteWidget::addSwatch(
int colorIndex)
const
207 QPixmap originalColorSwatch(mIconSize);
208 QPainter painter(&originalColorSwatch);
209 painter.drawTiledPixmap(0, 0, mIconSize.
width(), mIconSize.
height(),
QPixmap(
":/background/checkerboard.png"));
214 borderShadow.setDashPattern(dashPattern);
216 QPen borderHighlight(borderShadow);
217 borderHighlight.setColor(
QColor(255, 255, 255, 200));
218 borderHighlight.setDashOffset(4);
220 const ColorRef colorRef = mObject->getColor(colorIndex);
225 colorItem->
setText(colorRef.name);
232 QPixmap colorSwatch = originalColorSwatch;
233 QPainter swatchPainter(&colorSwatch);
234 swatchPainter.fillRect(0, 0, mIconSize.
width(), mIconSize.
height(), colorRef.color);
242 swatchPainter.setPen(borderHighlight);
243 swatchPainter.drawRect(0, 0, mIconSize.
width() - 1, mIconSize.
height() - 1);
244 swatchPainter.setPen(borderShadow);
245 swatchPainter.drawRect(0, 0, mIconSize.
width() - 1, mIconSize.
height() - 1);
249 colorItem->
setIcon(swatchIcon);
252 ui->colorListWidget->addItem(colorItem);
257 Q_ASSERT(item !=
nullptr);
261 int colorNumber = ui->colorListWidget->row(item);
262 if (colorNumber > -1)
269 mObject->getColor(colorNumber).name,
273 mObject->renameColor(colorNumber, text);
282 int index = ui->colorListWidget->row(item);
284 mObject->renameColor(index, newColorName);
287void ColorPaletteWidget::onRowsMoved(
const QModelIndex &parent,
int start,
int end,
const QModelIndex &destination,
int row)
290 Q_UNUSED(destination)
293 int startIndex, endIndex;
297 if (start == row) {
return; }
302 mObject->movePaletteColor(startIndex, endIndex);
304 mObject->addColor(mObject->getColor(startIndex));
305 mObject->moveVectorColor(startIndex, mObject->getColorCount() - 1);
306 for (
int i = startIndex; i < endIndex; i++)
308 mObject->moveVectorColor(i + 1, i);
310 mObject->moveVectorColor(mObject->getColorCount() - 1, endIndex);
314 if (start == row) {
return; }
319 mObject->movePaletteColor(startIndex, endIndex);
321 mObject->addColor(mObject->getColor(startIndex));
322 mObject->moveVectorColor(startIndex, mObject->getColorCount() - 1);
323 for (
int i = startIndex; i > endIndex; i--)
325 mObject->moveVectorColor(i - 1, i);
327 mObject->moveVectorColor(mObject->getColorCount() - 1, endIndex);
330 mObject->removeColor(mObject->getColorCount() - 1);
335void ColorPaletteWidget::clickColorListItem(
QListWidgetItem* currentItem)
337 auto modifiers = qApp->keyboardModifiers();
343 int colorIndex = ui->colorListWidget->row(currentItem);
345 emit colorNumberChanged(colorIndex);
348void ColorPaletteWidget::palettePreferences()
350 ui->colorListWidget->setMinimumWidth(ui->colorListWidget->sizeHintForColumn(0));
353 mSeparator =
new QAction(
"",
this);
356 buttonStylesheet =
"::menu-indicator{ image: none; }"
357 "QToolButton { border: 0px; }"
358 "QToolButton:pressed { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }"
359 "QToolButton:checked { border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
363 ui->palettePref->setIconSize(
QSize(22,22));
364 ui->palettePref->setArrowType(Qt::ArrowType::NoArrow);
365 ui->palettePref->setStyleSheet(buttonStylesheet);
366 ui->palettePref->addAction(ui->listModeAction);
367 ui->palettePref->addAction(ui->gridModeAction);
369 ui->palettePref->addAction(mSeparator);
370 ui->palettePref->addAction(ui->smallSwatchAction);
371 ui->palettePref->addAction(ui->mediumSwatchAction);
372 ui->palettePref->addAction(ui->largeSwatchAction);
373 ui->palettePref->addAction(ui->fitSwatchAction);
375 if (mFitSwatches) ui->fitSwatchAction->setChecked(
true);
376 else if (mIconSize.
width() > MEDIUM_ICON_SIZE) ui->largeSwatchAction->setChecked(
true);
377 else if (mIconSize.
width() > MIN_ICON_SIZE) ui->mediumSwatchAction->setChecked(
true);
378 else ui->smallSwatchAction->setChecked(
true);
381 ui->listModeAction->setChecked(
true);
383 ui->gridModeAction->setChecked(
true);
393void ColorPaletteWidget::setListMode()
397 ui->colorListWidget->setGridSize(
QSize(-1, -1));
405 settings.setValue(
"ColorPaletteViewMode",
"ListMode");
408void ColorPaletteWidget::setGridMode()
413 ui->colorListWidget->setGridSize(
QSize(mIconSize.
width() + 1, mIconSize.
height() + 1));
421 settings.setValue(
"ColorPaletteViewMode",
"GridMode");
424void ColorPaletteWidget::setSwatchSizeSmall()
426 if (mIconSize.
width() > MIN_ICON_SIZE)
428 mIconSize =
QSize(MIN_ICON_SIZE, MIN_ICON_SIZE);
431 mFitSwatches =
false;
433 settings.setValue(
"PreferredColorGridSize", MIN_ICON_SIZE);
434 settings.setValue(
"FitSwatchSize",
false);
438void ColorPaletteWidget::setSwatchSizeMedium()
440 if (mIconSize.
width() != MEDIUM_ICON_SIZE)
442 mIconSize =
QSize(MEDIUM_ICON_SIZE, MEDIUM_ICON_SIZE);
445 mFitSwatches =
false;
447 settings.setValue(
"PreferredColorGridSize", MEDIUM_ICON_SIZE);
448 settings.setValue(
"FitSwatchSize",
false);
452void ColorPaletteWidget::setSwatchSizeLarge()
454 if (mIconSize.
width() < MAX_ICON_SIZE)
456 mIconSize =
QSize(MAX_ICON_SIZE, MAX_ICON_SIZE);
459 mFitSwatches =
false;
461 settings.setValue(
"PreferredColorGridSize", MAX_ICON_SIZE);
462 settings.setValue(
"FitSwatchSize",
false);
466void ColorPaletteWidget::adjustSwatches()
472void ColorPaletteWidget::fitSwatchSize()
474 int height = ui->colorListWidget->height();
475 int width = ui->colorListWidget->width();
476 int hScrollBar = ui->colorListWidget->horizontalScrollBar()->geometry().height() + 6;
477 int vScrollBar = ui->colorListWidget->verticalScrollBar()->geometry().width() * 2;
478 int colorCount = editor()->object()->getColorCount();
484 if (
size < MIN_ICON_SIZE)
size = MIN_ICON_SIZE;
485 if (
size > MAX_ICON_SIZE)
size = MAX_ICON_SIZE;
490 size = MIN_ICON_SIZE;
493 int columns = (
width - vScrollBar) /
size;
494 int rows =
static_cast<int>(qCeil(
colorCount / columns));
498 if (
size == MAX_ICON_SIZE)
515 settings.setValue(
"PreferredColorGridSize",
size);
516 settings.setValue(
"FitSwatchSize",
true);
519void ColorPaletteWidget::resizeEvent(
QResizeEvent* event)
529void ColorPaletteWidget::updateGridUI()
533 for (
int i = 1; i < 75; i++)
535 int size = (ui->colorListWidget->width() - 18) / i;
543 ui->colorListWidget->setIconSize(
QSize(tempSize.
width(), mIconSize.
height()));
544 ui->colorListWidget->setGridSize(
QSize(tempSize.
width(), mIconSize.
height() + 2));
549 ui->colorListWidget->setIconSize(mIconSize);
550 ui->colorListWidget->setGridSize(
QSize(-1, -1));
554void ColorPaletteWidget::clickColorDialogButton()
556 mIsColorDialog =
true;
557 clickAddColorButton();
558 mIsColorDialog =
false;
561void ColorPaletteWidget::clickAddColorButton()
570 newColor = mEditor->color()->
frontColor(
false);
577 int colorIndex = mObject->getColorCount();
580 mObject->addColor(ref);
583 editor()->color()->setColorNumber(colorIndex);
591void ColorPaletteWidget::clickRemoveColorButton()
593 for (
auto item : ui->colorListWidget->selectedItems())
595 int index = ui->colorListWidget->row(item);
601 if (mObject->getColorCount() == 1)
603 showPaletteReminder();
607 bool colorRemoved =
false;
608 if (mObject->isColorInUse(index))
610 bool accepted =
false;
611 if (!mMultipleSelected)
612 accepted = showPaletteWarning();
614 if ((accepted || mMultipleSelected) && mObject->getColorCount() > 1)
617 mObject->removeColor(index);
621 else if (mObject->getColorCount() > 1)
624 mObject->removeColor(index);
629 int newIndex = qBound(0, index-1, mObject->getColorCount() - 1);
630 emit colorNumberChanged(newIndex);
633 mMultipleSelected =
false;
641bool ColorPaletteWidget::showPaletteWarning()
644 msgBox.
setText(
tr(
"The color(s) you are about to delete are currently being used by one or multiple strokes."));
651 if (ui->colorListWidget->selectedItems().size() > 1)
653 mMultipleSelected =
true;
660void ColorPaletteWidget::showPaletteReminder()
663 tr(
"The palette requires at least one swatch to remain functional"));
666void ColorPaletteWidget::updateItemColor(
int itemIndex,
QColor newColor)
668 QPixmap colorSwatch(mIconSize);
669 QPainter swatchPainter(&colorSwatch);
670 swatchPainter.drawTiledPixmap(0, 0, mIconSize.
width(), mIconSize.
height(),
QPixmap(
":/background/checkerboard.png"));
671 swatchPainter.fillRect(0, 0, mIconSize.
width(), mIconSize.
height(), newColor);
675 dashPattern << 4 << 4;
676 borderShadow.setDashPattern(dashPattern);
677 QPen borderHighlight(borderShadow);
678 borderHighlight.setColor(
QColor(255, 255, 255, 200));
679 borderHighlight.setDashOffset(4);
687 swatchPainter.setPen(borderHighlight);
688 swatchPainter.drawRect(0, 0, mIconSize.
width() - 1, mIconSize.
height() - 1);
689 swatchPainter.setPen(borderShadow);
690 swatchPainter.drawRect(0, 0, mIconSize.
width() - 1, mIconSize.
height() - 1);
694 ui->colorListWidget->item(itemIndex)->setIcon(swatchIcon);
695 editor()->object()->setColor(itemIndex, newColor);
void setIndexedColor(const QColor &newColor)
Set color for current index.
QColor frontColor(bool useIndexedColor=true)
frontColor
void updateFrame()
Will call update() and update the canvas Only call this directly If you need the cache to be intact a...
void rowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
void setSeparator(bool b)
void triggered(bool checked)
bool isValid() const const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QColorDialog::ColorDialogOptions options)
void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
void setText(const QString &text)
QString text() const const
virtual int exec() override
void setText(const QString &text)
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
int colorCount() const const
bool isEmpty() const const