Permalink
Browse files

Merge pull request #602 from CandyFace/paletteWidget-changes

Palette UI changes
  • Loading branch information...
2 parents ef9afb8 + c1b954e commit a200ee8d996eeb5f59959f414755a28fe9f9d598 @chchwy chchwy committed on GitHub Feb 12, 2017
Showing with 322 additions and 40 deletions.
  1. +183 −30 app/colorpalettewidget.cpp
  2. +25 −0 app/colorpalettewidget.h
  3. +114 −10 app/ui/colorpalette.ui
@@ -51,6 +51,9 @@ ColorPaletteWidget::ColorPaletteWidget( QWidget* pParent ) : BaseDockWidget( pPa
connect( ui->addColorButton, &QPushButton::clicked, this, &ColorPaletteWidget::clickAddColorButton );
connect( ui->removeColorButton, &QPushButton::clicked, this, &ColorPaletteWidget::clickRemoveColorButton );
+ connect( ui->palettePref, &QToolButton::clicked, this, &ColorPaletteWidget::palettePreferences );
+
+ ui->colorListWidget->setStyleSheet(QString("QListWidget { background-image: url(:/background/checkerboard.png); } "));
}
void ColorPaletteWidget::initUI()
@@ -59,13 +62,16 @@ void ColorPaletteWidget::initUI()
// vector strokes that are linked to palette
// colors don't handle color removal from palette
//
+ iconSize = QSize(34,34);
ui->removeColorButton->hide();
updateUI();
+ palettePreferences();
}
void ColorPaletteWidget::updateUI()
{
refreshColorList();
+ updateGridUI();
}
void ColorPaletteWidget::setColor(QColor newColor)
@@ -92,22 +98,24 @@ int ColorPaletteWidget::currentColourNumber()
void ColorPaletteWidget::refreshColorList()
{
- int r, g, b;
if ( ui->colorListWidget->count() > 0)
{
ui->colorListWidget->clear();
}
-
+
+ QListWidgetItem* colourItem;
+ ColourRef colourRef;
for (int i = 0; i < editor()->object()->getColourCount(); i++)
{
- ColourRef colourRef = editor()->object()->getColour(i);
+ colourRef = editor()->object()->getColour(i);
+ colourItem = new QListWidgetItem( ui->colorListWidget );
- QListWidgetItem* colourItem = new QListWidgetItem( ui->colorListWidget );
- colourItem->setText( colourRef.name );
-
- QPixmap colourSwatch(32, 32);
+ if ( ui->colorListWidget->viewMode() != QListView::IconMode){
+ colourItem->setText( colourRef.name );
+ }
+ colourSwatch = QPixmap(iconSize);
colourSwatch.fill( colourRef.colour );
colourItem->setIcon( colourSwatch );
@@ -155,6 +163,131 @@ void ColorPaletteWidget::changeColourName( QListWidgetItem* item )
}
}
+void ColorPaletteWidget::palettePreferences()
+{
+ layoutModes = new QActionGroup(this);
+ listMode = new QAction (tr("List mode"), this);
+ listMode->setStatusTip(tr("Show palette as a list"));
+ listMode->setCheckable(true);
+ listMode->setChecked(true);
+ layoutModes->addAction(listMode);
+ connect(listMode, &QAction::triggered, this, &ColorPaletteWidget::setListMode);
+
+ gridMode = new QAction (tr("Grid mode"), this);
+ gridMode->setStatusTip(tr("Show palette as icons"));
+ gridMode->setCheckable(true);
+ layoutModes->addAction(gridMode);
+ connect(gridMode, &QAction::triggered, this, &ColorPaletteWidget::setGridMode);
+
+ // Swatch size control
+ iconSizes = new QActionGroup (this);
+ smallSwatch = new QAction(tr("Small swatch"), this);
+ smallSwatch->setStatusTip(tr("Sets swatch size to: 16x16px"));
+ smallSwatch->setCheckable(true);
+ iconSizes->addAction(smallSwatch);
+ connect(smallSwatch, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeSmall);
+
+ mediumSwatch = new QAction(tr("Medium swatch"), this);
+ mediumSwatch->setStatusTip(tr("Sets swatch size to: 26x26px"));
+ mediumSwatch->setCheckable(true);
+ iconSizes->addAction(mediumSwatch);
+ connect(mediumSwatch, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeMedium);
+
+ largeSwatch = new QAction(tr("Large swatch"), this);
+ largeSwatch->setCheckable(true);
+ largeSwatch->setStatusTip(tr("Sets swatch size to: 36x36px"));
+ iconSizes->addAction(largeSwatch);
+ largeSwatch->setChecked(true);
+ connect(largeSwatch, &QAction::triggered, this, &ColorPaletteWidget::setSwatchSizeLarge);
+
+ ui->colorListWidget->setMinimumWidth(ui->colorListWidget->sizeHintForColumn(0));
+
+ // Let's pretend this button is a separator
+ separator = new QAction(tr(""), this);
+ separator->setSeparator(true);
+
+ // Add to UI
+ ui->palettePref->addAction(listMode);
+ ui->palettePref->addAction(gridMode);
+ ui->palettePref->addAction(separator);
+ ui->palettePref->addAction(smallSwatch);
+ ui->palettePref->addAction(mediumSwatch);
+ ui->palettePref->addAction(largeSwatch);
+}
+
+void ColorPaletteWidget::setListMode()
+{
+ ui->colorListWidget->setViewMode(QListView::ListMode);
+ ui->colorListWidget->setMovement(QListView::Static);
+ ui->colorListWidget->setGridSize(QSize(-1,-1));
+ updateUI();
+}
+
+void ColorPaletteWidget::setGridMode()
+{
+ ui->colorListWidget->setViewMode(QListView::IconMode);
+ ui->colorListWidget->setMovement(QListView::Static); // TODO: update swatch index on move
+ ui->colorListWidget->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
+ ui->colorListWidget->setGridSize(iconSize);
+ updateUI();
+}
+
+void ColorPaletteWidget::resizeEvent(QResizeEvent *event)
+{
+ // Find the value to divivde with
+ for (int i = 1; i < 75; i++) {
+ int size = (ui->colorListWidget->width()-18) / i; // subtract scrollbar width
+ if (size >= iconSize.width() && size <= iconSize.width() + 8){
+ stepper = size;
+ }
+ }
+ QSize tempSize = QSize(stepper,iconSize.height());
+
+ ui->colorListWidget->setIconSize(QSize(tempSize.width(),iconSize.height()));
+ ui->colorListWidget->setGridSize(QSize(tempSize.width(),iconSize.height()));
+ iconSize.setWidth(iconSize.width());
+
+ refreshColorList();
+ QWidget::resizeEvent(event);
+}
+
+void ColorPaletteWidget::setSwatchSizeSmall()
+{
+ if (iconSize.width() > 18) {
+ iconSize = QSize(14,14);
+ }
+ updateUI();
+}
+
+void ColorPaletteWidget::setSwatchSizeMedium()
+{
+ if (iconSize.width() < 20 || iconSize.width() > 30) {
+ iconSize = QSize(26,26);
+ updateUI();
+ }
+}
+
+void ColorPaletteWidget::setSwatchSizeLarge()
+{
+ if (iconSize.width() < 30) {
+ iconSize = QSize(34,34);
+ updateUI();
+ }
+}
+
+void ColorPaletteWidget::updateGridUI()
+{
+ if (ui->colorListWidget->viewMode() == QListView::IconMode)
+ {
+ ui->colorListWidget->setGridSize(QSize(iconSize.width(),iconSize.height()));
+ }
+ else {
+
+ ui->colorListWidget->setGridSize(QSize(-1,-1));
+ }
+ ui->colorListWidget->setIconSize(iconSize);
+}
+
void ColorPaletteWidget::clickAddColorButton()
{
QColor prevColor = Qt::white;
@@ -166,31 +299,45 @@ void ColorPaletteWidget::clickAddColorButton()
bool ok;
QRgb qrgba = QColorDialog::getRgba( prevColor.rgba(), &ok, this );
- if ( ok )
- {
- ColourRef ref = ColourRef(QColor::fromRgba(qrgba));
- bool ok;
- QString text = QInputDialog::getText(this,
- tr("Colour name"),
- tr("Colour name:"),
- QLineEdit::Normal,
- QString(tr("Colour %1")).arg( ui->colorListWidget->count()),
- &ok );
- if (ok)
+ ColourRef ref;
+ QString text;
+ if (ui->colorListWidget->viewMode() != QListView::IconMode){
+ if ( ok )
{
- ref.name = text;
- editor()->object()->addColour(ref);
- refreshColorList();
-
- int colorIndex = editor()->object()->getColourCount() - 1;
+ ref = ColourRef(QColor::fromRgba(qrgba));
+ bool ok;
+ text = QInputDialog::getText(this,
+ tr("Colour name"),
+ tr("Colour name:"),
+ QLineEdit::Normal,
+ QString(tr("Colour %1")).arg( ui->colorListWidget->count()),
+ &ok );
+ if (ok)
+ {
+ ref.name = text;
+ editor()->object()->addColour(ref);
+ refreshColorList();
+
+ int colorIndex = editor()->object()->getColourCount() - 1;
+
+ editor()->color()->setColorNumber(colorIndex);
+ editor()->color()->setColor( ref.colour );
+
+ // This is done through editor()->color()->setColorNumber()
+ //
+ //emit colorNumberChanged( colorIndex );
+ }
+ }
+ }
+ else{
+ ref = ColourRef(QColor::fromRgba(qrgba));
+ editor()->object()->addColour(ref);
+ refreshColorList();
- editor()->color()->setColorNumber(colorIndex);
- editor()->color()->setColor( ref.colour );
+ int colorIndex = editor()->object()->getColourCount() - 1;
- // This is done through editor()->color()->setColorNumber()
- //
- //emit colorNumberChanged( colorIndex );
- }
+ editor()->color()->setColorNumber(colorIndex);
+ editor()->color()->setColor( ref.colour );
}
}
@@ -204,7 +351,13 @@ void ColorPaletteWidget::clickRemoveColorButton()
void ColorPaletteWidget::updateItemColor( int itemIndex, QColor newColor )
{
- QPixmap colourSwatch(32, 32);
+ colourSwatch = QPixmap(iconSize);
colourSwatch.fill( newColor );
ui->colorListWidget->item( itemIndex )->setIcon( colourSwatch );
+
+ // Make sure to update grid in grid mode
+ if (ui->colorListWidget->viewMode() == QListView::IconMode)
+ {
+ updateGridUI();
+ }
}
@@ -25,6 +25,8 @@ class QListWidgetItem;
class Object;
class Editor;
class ColorBox;
+class QActionGroup;
+class QMenu;
namespace Ui
{
@@ -49,6 +51,9 @@ public slots:
void setColor(QColor);
void refreshColorList();
+protected:
+ void resizeEvent(QResizeEvent *event) override;
+
signals:
void colorChanged(QColor);
void colorNumberChanged(int);
@@ -60,9 +65,29 @@ private slots:
void changeColourName(QListWidgetItem*);
void clickAddColorButton();
void clickRemoveColorButton();
+ void palettePreferences();
+ void setListMode();
+ void setGridMode();
+ void setSwatchSizeSmall();
+ void setSwatchSizeMedium();
+ void setSwatchSizeLarge();
+ void updateGridUI();
private:
Ui::ColorPalette* ui = nullptr;
+ QActionGroup *layoutModes;
+ QAction *listMode;
+ QAction *gridMode;
+ QActionGroup *iconSizes;
+ QAction *smallSwatch;
+ QAction *mediumSwatch;
+ QAction *largeSwatch;
+ QAction *separator;
+ QSize iconSize;
+ QPixmap colourSwatch;
+ QMenu *toolMenu;
+ int stepper;
+
};
#endif
Oops, something went wrong.

0 comments on commit a200ee8

Please sign in to comment.