Skip to content

Commit

Permalink
Merge pull request #1264 from davidlamhauge/swatch_slider
Browse files Browse the repository at this point in the history
Swatches can be set to fit in widget - if possible
  • Loading branch information
candyface authored Feb 27, 2020
2 parents cf7f925 + e06617d commit f3ce5d9
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 24 deletions.
133 changes: 113 additions & 20 deletions app/src/colorpalettewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ GNU General Public License for more details.
#include <QPushButton>
#include <QSettings>
#include <QMenu>
#include <QtMath>
#include <QScrollBar>
#include <QAbstractItemModel>

// Project
Expand Down Expand Up @@ -60,6 +62,11 @@ void ColorPaletteWidget::initUI()
{
QSettings settings(PENCIL2D, PENCIL2D);
int colorGridSize = settings.value("PreferredColorGridSize", 34).toInt();
mFitSwatches = settings.value("FitSwatchSize", false).toBool();
if (mFitSwatches)
{
fitSwatchSize();
}

mIconSize = QSize(colorGridSize, colorGridSize);

Expand Down Expand Up @@ -133,6 +140,10 @@ void ColorPaletteWidget::addItem()

mObject->addColourAtIndex(colorIndex, ref);
refreshColorList();
if (mFitSwatches)
{
fitSwatchSize();
}
}

void ColorPaletteWidget::replaceItem()
Expand Down Expand Up @@ -357,13 +368,16 @@ void ColorPaletteWidget::palettePreferences()
ui->palettePref->setStyleSheet(buttonStylesheet);
ui->palettePref->addAction(ui->listModeAction);
ui->palettePref->addAction(ui->gridModeAction);

ui->palettePref->addAction(mSeparator);
ui->palettePref->addAction(ui->smallSwatchAction);
ui->palettePref->addAction(ui->mediumSwatchAction);
ui->palettePref->addAction(ui->largeSwatchAction);
ui->palettePref->addAction(ui->fitSwatchAction);

if (mIconSize.width() > 30) ui->largeSwatchAction->setChecked(true);
else if (mIconSize.width() > 20) ui->mediumSwatchAction->setChecked(true);
if (mFitSwatches) ui->fitSwatchAction->setChecked(true);
else if (mIconSize.width() > MEDIUM_ICON_SIZE) ui->largeSwatchAction->setChecked(true);
else if (mIconSize.width() > MIN_ICON_SIZE) ui->mediumSwatchAction->setChecked(true);
else ui->smallSwatchAction->setChecked(true);

if (ui->colorListWidget->viewMode() == QListView::ListMode)
Expand All @@ -373,6 +387,7 @@ void ColorPaletteWidget::palettePreferences()

connect(ui->listModeAction, &QAction::triggered, this, &ColorPaletteWidget::setListMode);
connect(ui->gridModeAction, &QAction::triggered, this, &ColorPaletteWidget::setGridMode);
connect(ui->fitSwatchAction, &QAction::triggered, this, &ColorPaletteWidget::fitSwatchSize);
connect(ui->smallSwatchAction, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeSmall);
connect(ui->mediumSwatchAction, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeMedium);
connect(ui->largeSwatchAction, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeLarge);
Expand All @@ -383,6 +398,10 @@ void ColorPaletteWidget::setListMode()
ui->colorListWidget->setViewMode(QListView::ListMode);
ui->colorListWidget->setDragDropMode(QAbstractItemView::InternalMove);
ui->colorListWidget->setGridSize(QSize(-1, -1));
if (mFitSwatches)
{
fitSwatchSize();
}
updateUI();

QSettings settings(PENCIL2D, PENCIL2D);
Expand All @@ -395,55 +414,121 @@ void ColorPaletteWidget::setGridMode()
ui->colorListWidget->setMovement(QListView::Static); // TODO: update swatch index on move
ui->colorListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->colorListWidget->setGridSize(QSize(mIconSize.width() + 1, mIconSize.height() + 1));

if (mFitSwatches)
{
fitSwatchSize();
}
updateUI();

QSettings settings(PENCIL2D, PENCIL2D);
settings.setValue("ColorPaletteViewMode", "GridMode");
}

void ColorPaletteWidget::resizeEvent(QResizeEvent* event)
{
updateUI();
QWidget::resizeEvent(event);
}

void ColorPaletteWidget::setSwatchSizeSmall()
{
if (mIconSize.width() > 18)
if (mIconSize.width() > MIN_ICON_SIZE)
{
mIconSize = QSize(14, 14);
mIconSize = QSize(MIN_ICON_SIZE, MIN_ICON_SIZE);
updateUI();

mFitSwatches = false;
QSettings settings(PENCIL2D, PENCIL2D);
settings.setValue("PreferredColorGridSize", 14);
settings.setValue("PreferredColorGridSize", MIN_ICON_SIZE);
settings.setValue("FitSwatchSize", false);
}
}

void ColorPaletteWidget::setSwatchSizeMedium()
{
if (mIconSize.width() < 20 || mIconSize.width() > 30)
if (mIconSize.width() != MEDIUM_ICON_SIZE)
{
mIconSize = QSize(26, 26);
mIconSize = QSize(MEDIUM_ICON_SIZE, MEDIUM_ICON_SIZE);
updateUI();

mFitSwatches = false;
QSettings settings(PENCIL2D, PENCIL2D);
settings.setValue("PreferredColorGridSize", 26);
settings.setValue("PreferredColorGridSize", MEDIUM_ICON_SIZE);
settings.setValue("FitSwatchSize", false);
}
}

void ColorPaletteWidget::setSwatchSizeLarge()
{
if (mIconSize.width() < 30)
if (mIconSize.width() < MAX_ICON_SIZE)
{
mIconSize = QSize(34, 34);
mIconSize = QSize(MAX_ICON_SIZE, MAX_ICON_SIZE);
updateUI();

mFitSwatches = false;
QSettings settings(PENCIL2D, PENCIL2D);
settings.setValue("PreferredColorGridSize", 34);
settings.setValue("PreferredColorGridSize", MAX_ICON_SIZE);
settings.setValue("FitSwatchSize", false);
}
}

void ColorPaletteWidget::adjustSwatches()
{
if (mFitSwatches)
fitSwatchSize();
}

void ColorPaletteWidget::fitSwatchSize()
{
int height = ui->colorListWidget->height();
int width = ui->colorListWidget->width();
int hScrollBar = ui->colorListWidget->horizontalScrollBar()->geometry().height() + 6;
int vScrollBar = ui->colorListWidget->verticalScrollBar()->geometry().width() * 2;
int colorCount = editor()->object()->getColourCount();
int size;

if (ui->colorListWidget->viewMode() == QListView::ListMode)
{
size = qFloor((height - hScrollBar - (4 * colorCount)) / colorCount);
if (size < MIN_ICON_SIZE) size = MIN_ICON_SIZE;
if (size > MAX_ICON_SIZE) size = MAX_ICON_SIZE;
}
else
{
bool proceed = true;
size = MIN_ICON_SIZE;
while (proceed)
{
int columns = (width - vScrollBar) / size;
int rows = static_cast<int>(qCeil(colorCount / columns));
if (height - hScrollBar > rows * (size + 6))
{
size++;
if (size == MAX_ICON_SIZE)
{
proceed = false;
}
}
else
{
proceed = false;
}
}
}
mIconSize = QSize(size, size);

updateUI();

mFitSwatches = true;
QSettings settings(PENCIL2D, PENCIL2D);
settings.setValue("PreferredColorGridSize", size);
settings.setValue("FitSwatchSize", true);
}

void ColorPaletteWidget::resizeEvent(QResizeEvent* event)
{
updateUI();
if (mFitSwatches)
{
fitSwatchSize();
}
QWidget::resizeEvent(event);
}

void ColorPaletteWidget::updateGridUI()
{
if (ui->colorListWidget->viewMode() == QListView::IconMode) {
Expand Down Expand Up @@ -498,8 +583,12 @@ void ColorPaletteWidget::clickAddColorButton()
mObject->addColour(ref);
refreshColorList();

mEditor->color()->setColorNumber(colorIndex);
mEditor->color()->setColor(ref.colour);
editor()->color()->setColorNumber(colorIndex);
editor()->color()->setColor(ref.colour);
if (mFitSwatches)
{
fitSwatchSize();
}
}

void ColorPaletteWidget::clickRemoveColorButton()
Expand Down Expand Up @@ -535,6 +624,10 @@ void ColorPaletteWidget::clickRemoveColorButton()
mEditor->updateCurrentFrame();
}
mMultipleSelected = false;
if (mFitSwatches)
{
fitSwatchSize();
}
}

bool ColorPaletteWidget::showPaletteWarning()
Expand Down
8 changes: 8 additions & 0 deletions app/src/colorpalettewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ColorPaletteWidget : public BaseDockWidget
void setColor(QColor, int);
void refreshColorList();

void adjustSwatches();

void showContextMenu(const QPoint&);

signals:
Expand All @@ -77,6 +79,7 @@ private slots:
void setSwatchSizeSmall();
void setSwatchSizeMedium();
void setSwatchSizeLarge();
void fitSwatchSize();
void addItem();
void replaceItem();
void removeItem();
Expand Down Expand Up @@ -106,6 +109,11 @@ private slots:

bool mIsColorDialog = false;
bool mMultipleSelected = false;
bool mFitSwatches = false;

const int MIN_ICON_SIZE = 19;
const int MEDIUM_ICON_SIZE = 26;
const int MAX_ICON_SIZE = 36;

Editor* mEditor = nullptr;
Object* mObject = nullptr;
Expand Down
1 change: 1 addition & 0 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ void MainWindow2::importPalette()
{
mEditor->object()->importPalette(filePath);
mColorPalette->refreshColorList();
mColorPalette->adjustSwatches();
mEditor->color()->setColorNumber(0);
}
}
Expand Down
31 changes: 27 additions & 4 deletions app/ui/colorpalette.ui
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<width>40</width>
<height>20</height>
</size>
</property>
Expand Down Expand Up @@ -216,6 +213,18 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
Expand Down Expand Up @@ -310,6 +319,20 @@
<string>Sets swatch size to: 36x36px</string>
</property>
</action>
<action name="fitSwatchAction">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Fit Swatch</string>
</property>
<property name="toolTip">
<string>Fit swatch to window (19-36 px)</string>
</property>
<property name="statusTip">
<string>Swatch fits window</string>
</property>
</action>
</actiongroup>
</widget>
<resources>
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/structure/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Object : public QObject

signals:
void layerViewChanged();
void paletteImported();

private:
int getMaxLayerID();
Expand Down

0 comments on commit f3ce5d9

Please sign in to comment.