[neon/backports-jammy/kcolorpicker] /: New upstream version 0.2.0
Boyuan Yang
null at kde.org
Tue Jan 16 14:07:27 GMT 2024
Git commit 29ca4f980d66935e5e5d9372b99cccccd056bb8a by Boyuan Yang.
Committed on 23/05/2022 at 21:38.
Pushed by jriddell into branch 'master'.
New upstream version 0.2.0
M +4 -1 .github/FUNDING.yml
A +33 -0 .github/workflows/build.yml
D +0 -28 .travis.yml
M +1 -1 CMakeLists.txt
M +7 -3 README.md
M +1 -1 example/main.cpp
M +2 -1 include/kColorPicker/KColorPicker.h
M +31 -4 src/IconCreator.cpp
M +2 -0 src/IconCreator.h
M +35 -19 src/KColorPicker.cpp
M +20 -6 src/PopupMenu.cpp
M +3 -1 src/PopupMenu.h
M +8 -1 src/buttons/ColorButton.cpp
M +2 -0 src/buttons/ColorButton.h
M +9 -2 src/buttons/ColorDialogButton.cpp
M +4 -1 src/buttons/ColorDialogButton.h
https://invent.kde.org/neon/backports-jammy/kcolorpicker/-/commit/29ca4f980d66935e5e5d9372b99cccccd056bb8a
diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
index c54cb46..ed88f56 100644
--- a/.github/FUNDING.yml
+++ b/.github/FUNDING.yml
@@ -1,4 +1,7 @@
# These are supported funding model platforms
-github: [DamirPorobic]
+github: DamirPorobic
+liberapay: dporobic
+patreon: dporobic
+open_collective: ksnip
custom: paypal.me/damirporobic
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..8fcb6a2
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,33 @@
+name: Build
+
+on:
+ push:
+ branches: [ master ]
+ pull_request:
+ branches: [ master ]
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout at v2
+
+ - name: Install Qt
+ uses: jurplel/install-qt-action at v2
+ with:
+ version: '5.15.2'
+ host: 'linux'
+ install-deps: 'true'
+
+ - name: Install dependencies
+ run: sudo apt-get install xvfb
+
+ - name: Build
+ run: |
+ mkdir build && cd build
+ cmake .. -DBUILD_TESTS=ON && make VERBOSE=1
+
+ - name: Test
+ working-directory: ${{github.workspace}}/build
+ run: xvfb-run make test CTEST_OUTPUT_ON_FAILURE=1
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 483db86..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,28 +0,0 @@
-language: cpp
-compiler: gcc
-sudo: required
-dist: trusty
-
-before_install:
-- sudo add-apt-repository ppa:beineri/opt-qt594-trusty -y
-- sudo apt-get update -qq
-
-install:
-- sudo apt-get -y install qt59base
-- source /opt/qt*/bin/qt*-env.sh
-
-jobs:
- include:
- - stage: build
- script:
- - mkdir build
- - cd build
- - cmake ..
- - make VERBOSE=1
- - stage: test
- script:
- - mkdir build
- - cd build
- - cmake .. -DBUILD_TESTS=ON
- - make
- - xvfb-run make test CTEST_OUTPUT_ON_FAILURE=1
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f78a5a7..d412f33 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5)
-project(kColorPicker LANGUAGES CXX VERSION 0.1.6)
+project(kColorPicker LANGUAGES CXX VERSION 0.2.0)
set(QT_MIN_VERSION "5.9.4")
diff --git a/README.md b/README.md
index 6d06c4b..c126451 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,12 @@
-# kColorPicker [![Build Status](https://travis-ci.org/ksnip/kColorPicker.svg?branch=master)](https://travis-ci.org/ksnip/kColorPicker)
+# kColorPicker [![Build Status][github-badge]][github-url]
QToolButton with color popup menu with lets you select a color. The popup features a color dialog button which can be used to add custom colors to the popup menu.
-Version 0.1.6
+Version 0.2.0
![kColorPicker](https://i.imgur.com/VkhUvFa.png "kColorPicker")
### Building from source
-1. Get latest release from GitHub by cloning the repo:
+1. Get the latest release from GitHub by cloning the repo:
`$ git clone https://github.com/ksnip/kColorPicker`
2. Change to repo directory:
`$ cd kColorPicker`
@@ -34,3 +34,7 @@ only build as static library.
2. Link the library with your application
`target_link_libraries(myApp kColorPicker)`
+
+
+[github-badge]: https://github.com/ksnip/kColorPicker/actions/workflows/build.yml/badge.svg
+[github-url]: https://github.com/ksnip/kColorPicker/actions
\ No newline at end of file
diff --git a/example/main.cpp b/example/main.cpp
index 2d42bb4..c562cf2 100644
--- a/example/main.cpp
+++ b/example/main.cpp
@@ -30,7 +30,7 @@ int main(int argc, char **argv)
widget->setFixedWidth(200);
widget->setFixedHeight(200);
auto layout = new QVBoxLayout();
- auto colorPicker = new KColorPicker();
+ auto colorPicker = new KColorPicker(true);
colorPicker->setColor(QColor(Qt::red));
layout->addWidget(colorPicker);
widget->setLayout(layout);
diff --git a/include/kColorPicker/KColorPicker.h b/include/kColorPicker/KColorPicker.h
index e57e6f3..9309bb3 100644
--- a/include/kColorPicker/KColorPicker.h
+++ b/include/kColorPicker/KColorPicker.h
@@ -37,11 +37,12 @@ Q_OBJECT
Q_DECLARE_PRIVATE(KColorPicker)
public:
- explicit KColorPicker();
+ explicit KColorPicker(bool showAlphaChannel = false, QWidget *parent = nullptr);
~KColorPicker() override;
void setFixedSize(const QSize &size);
void setFixedSize(int width, int height);
QColor color() const;
+ void resetColors(bool showAlphaChannel = false);
public Q_SLOTS:
void setColor(const QColor &color);
diff --git a/src/IconCreator.cpp b/src/IconCreator.cpp
index 4250600..dd21cfd 100644
--- a/src/IconCreator.cpp
+++ b/src/IconCreator.cpp
@@ -23,14 +23,41 @@ namespace kColorPicker {
QIcon IconCreator::createIcon(const QColor &color, const QSize &size)
{
- QPixmap pixmap(size);
- pixmap.fill(color);
- QPainter painter(&pixmap);
+ auto background = getBackgroundBrush(size);
+
+ QPixmap pixmap(size);
+ QPainter painter(&pixmap);
+
+ // Paint background
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(background);
+ painter.drawRect(0, 0, size.width(), size.height());
+
+ // Paint color
+ painter.setBrush(color);
+ painter.drawRect(0, 0, size.width(), size.height());
+
+ // Paint border
auto penWidth = painter.pen().width();
painter.setPen(QColor(Qt::gray));
painter.drawRect(0, 0, size.width() - penWidth, size.height() - penWidth);
- return QIcon(pixmap);
+ return { pixmap };
+}
+
+QImage IconCreator::getBackgroundBrush(const QSize &size)
+{
+ auto halfWidth = size.width() / 2;
+ auto halfHeight = size.height() / 2;
+ auto background = QImage(size, QImage::Format_ARGB32_Premultiplied);
+ background.fill(Qt::white);
+ QPainter painter(&background);
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(Qt::gray);
+ painter.drawRect(0, 0, halfWidth, halfHeight);
+ painter.drawRect(halfWidth, halfHeight, size.width(), size.height());
+
+ return background;
}
} // namespace kColorPicker
\ No newline at end of file
diff --git a/src/IconCreator.h b/src/IconCreator.h
index 1d8888f..9e7061e 100644
--- a/src/IconCreator.h
+++ b/src/IconCreator.h
@@ -23,6 +23,7 @@
#include <QIcon>
#include <QColor>
#include <QPainter>
+#include <QImage>
namespace kColorPicker {
@@ -32,6 +33,7 @@ public:
explicit IconCreator() = delete;
~IconCreator() = delete;
static QIcon createIcon(const QColor &color, const QSize &size);
+ static QImage getBackgroundBrush(const QSize &size);
};
} // namespace kColorPicker
diff --git a/src/KColorPicker.cpp b/src/KColorPicker.cpp
index 474e3b4..bc7344a 100644
--- a/src/KColorPicker.cpp
+++ b/src/KColorPicker.cpp
@@ -35,16 +35,19 @@ class KColorPickerPrivate
Q_DECLARE_PUBLIC(KColorPicker)
- explicit KColorPickerPrivate(KColorPicker *kColorPicker);
+ explicit KColorPickerPrivate(KColorPicker *kColorPicker, bool showAlphaChannel = false, QWidget *parent = nullptr);
KColorPicker *const q_ptr;
QSize mIconSize;
- PopupMenu mPopupMenu;
+ PopupMenu *mPopupMenu;
QColor mSelectedColor;
};
-KColorPicker::KColorPicker() : d_ptr(new KColorPickerPrivate(this))
-{}
+KColorPicker::KColorPicker(bool showAlphaChannel, QWidget *parent) : d_ptr(new KColorPickerPrivate(this, showAlphaChannel, parent))
+{
+ // Default Colors
+ resetColors(showAlphaChannel);
+}
KColorPicker::~KColorPicker()
{}
@@ -54,7 +57,7 @@ void KColorPicker::setColor(const QColor &color)
Q_D(KColorPicker);
d->mSelectedColor = color;
setColorIcon(color);
- d->mPopupMenu.selectColor(color);
+ d->mPopupMenu->selectColor(color);
}
void KColorPicker::colorSelected(const QColor &color)
@@ -87,6 +90,28 @@ QColor KColorPicker::color() const
return d->mSelectedColor;
}
+void KColorPicker::resetColors(bool showAlphaChannel)
+{
+ Q_D(const KColorPicker);
+ d->mPopupMenu->removeColors();
+
+ d->mPopupMenu->addColor(QColor(Qt::red));
+ d->mPopupMenu->addColor(QColor(Qt::green));
+ d->mPopupMenu->addColor(QColor(Qt::blue));
+ d->mPopupMenu->addColor(QColor(Qt::yellow));
+ d->mPopupMenu->addColor(QColor(Qt::magenta));
+ d->mPopupMenu->addColor(QColor(Qt::cyan));
+ d->mPopupMenu->addColor(QColor(Qt::white));
+ d->mPopupMenu->addColor(QColor(Qt::black));
+
+ if(showAlphaChannel) {
+ d->mPopupMenu->addColor(QColor(0, 255, 255, 100));
+ d->mPopupMenu->addColor(QColor(255, 0, 255, 100));
+ d->mPopupMenu->addColor(QColor(255, 255, 0, 100));
+ d->mPopupMenu->addColor(QColor(255, 255, 255, 100));
+ }
+}
+
void KColorPicker::setIconSize(const QSize &size)
{
Q_D(KColorPicker);
@@ -107,25 +132,16 @@ void KColorPicker::setColorIcon(const QColor &color)
// KColorPickerPrivate
//
-KColorPickerPrivate::KColorPickerPrivate(KColorPicker *kColorPicker) : q_ptr(kColorPicker)
+KColorPickerPrivate::KColorPickerPrivate(KColorPicker *kColorPicker, bool showAlphaChannel, QWidget *parent) :
+ q_ptr(kColorPicker),
+ mPopupMenu(new PopupMenu(showAlphaChannel, parent))
{
initResource();
mIconSize = QSize(25, 25);
kColorPicker->setPopupMode(QToolButton::InstantPopup);
- kColorPicker->setMenu(&mPopupMenu);
- kColorPicker->connect(&mPopupMenu, &PopupMenu::colorChanged, kColorPicker, &KColorPicker::colorSelected);
-
- // Default Colors
- mPopupMenu.addColor(QColor(Qt::red));
- mPopupMenu.addColor(QColor(Qt::green));
- mPopupMenu.addColor(QColor(Qt::blue));
- mPopupMenu.addColor(QColor(Qt::yellow));
- mPopupMenu.addColor(QColor(Qt::magenta));
- mPopupMenu.addColor(QColor(Qt::cyan));
- mPopupMenu.addColor(QColor(Qt::white));
- mPopupMenu.addColor(QColor(Qt::black));
-
+ kColorPicker->setMenu(mPopupMenu);
+ kColorPicker->connect(mPopupMenu, &PopupMenu::colorChanged, kColorPicker, &KColorPicker::colorSelected);
}
} // namespace kColorPicker
diff --git a/src/PopupMenu.cpp b/src/PopupMenu.cpp
index d279bfa..3a1296e 100644
--- a/src/PopupMenu.cpp
+++ b/src/PopupMenu.cpp
@@ -21,12 +21,12 @@
namespace kColorPicker {
-PopupMenu::PopupMenu() :
- mButtonGroup(new QButtonGroup()),
- mLayout(new QGridLayout()),
- mColorDialogButton(new ColorDialogButton(QIcon(QLatin1String(":/icons/ellipsis"))))
+PopupMenu::PopupMenu(bool showAlphaChannel, QWidget *parent) :
+ QMenu(parent),
+ mButtonGroup(new QButtonGroup(this)),
+ mLayout(new QGridLayout(this)),
+ mColorDialogButton(new ColorDialogButton(QIcon(QLatin1String(":/icons/ellipsis")), showAlphaChannel))
{
-
mLayout->setSpacing(0);
mLayout->setMargin(5);
setLayout(mLayout);
@@ -36,7 +36,7 @@ PopupMenu::PopupMenu() :
PopupMenu::~PopupMenu()
{
- delete mButtonGroup;
+ qDeleteAll(mColorButtons);
delete mColorDialogButton;
}
@@ -47,6 +47,20 @@ void PopupMenu::addColor(const QColor &color)
}
}
+void PopupMenu::removeColors()
+{
+ for (auto button : mColorButtons) {
+ mButtonGroup->removeButton(button);
+ mLayout->removeWidget(button);
+ disconnect(button, &AbstractPopupMenuButton::colorSelected, this, &PopupMenu::colorSelected);
+ }
+
+ qDeleteAll(mColorButtons);
+ mColorButtons.clear();
+
+ generateGrid();
+}
+
void PopupMenu::selectColor(const QColor &color)
{
addColor(color);
diff --git a/src/PopupMenu.h b/src/PopupMenu.h
index 1171e40..4b6d937 100644
--- a/src/PopupMenu.h
+++ b/src/PopupMenu.h
@@ -34,9 +34,10 @@ class PopupMenu : public QMenu
{
Q_OBJECT
public:
- PopupMenu();
+ PopupMenu(bool showAlphaChannel, QWidget *parent);
~PopupMenu() override;
void addColor(const QColor &color);
+ void removeColors();
void selectColor(const QColor &color);
QSize sizeHint() const;
@@ -60,4 +61,5 @@ private slots:
};
} // namespace kColorPicker
+
#endif //KCOLORPICKER_POPUPMENU_H
diff --git a/src/buttons/ColorButton.cpp b/src/buttons/ColorButton.cpp
index d7c83a9..191d999 100644
--- a/src/buttons/ColorButton.cpp
+++ b/src/buttons/ColorButton.cpp
@@ -24,7 +24,7 @@ namespace kColorPicker {
ColorButton::ColorButton(const QIcon &icon, const QColor &color) : AbstractPopupMenuButton(icon)
{
setCheckable(true);
- setToolTip(color.name());
+ setToolTip(getColorName(color));
mColor = color;
}
@@ -38,4 +38,11 @@ void ColorButton::buttonClicked()
emit colorSelected(mColor);
}
+QString ColorButton::getColorName(const QColor &color)
+{
+ auto format = color.alpha() < 255 ? QColor::HexArgb : QColor::HexRgb;
+
+ return color.name(format);
+}
+
} // namespace kColorPicker
\ No newline at end of file
diff --git a/src/buttons/ColorButton.h b/src/buttons/ColorButton.h
index 84eee43..9e5d57b 100644
--- a/src/buttons/ColorButton.h
+++ b/src/buttons/ColorButton.h
@@ -36,6 +36,8 @@ protected:
private:
QColor mColor;
+
+ static QString getColorName(const QColor &color);
};
} // namespace kColorPicker
diff --git a/src/buttons/ColorDialogButton.cpp b/src/buttons/ColorDialogButton.cpp
index dbc3288..2868ca3 100644
--- a/src/buttons/ColorDialogButton.cpp
+++ b/src/buttons/ColorDialogButton.cpp
@@ -21,15 +21,22 @@
namespace kColorPicker {
-ColorDialogButton::ColorDialogButton(const QIcon &icon) : AbstractPopupMenuButton(icon)
+ColorDialogButton::ColorDialogButton(const QIcon &icon, bool showAlphaChannel) :
+ AbstractPopupMenuButton(icon),
+ mShowAlphaChannel(showAlphaChannel)
{
setCheckable(false);
}
void ColorDialogButton::buttonClicked()
{
- auto color = QColorDialog::getColor();
+ auto color = QColorDialog::getColor(Qt::white, this, QString(), getColorDialogOptions());
emit colorSelected(color);
}
+QFlags<QColorDialog::ColorDialogOption> ColorDialogButton::getColorDialogOptions() const
+{
+ return mShowAlphaChannel ? QColorDialog::ShowAlphaChannel : QFlags<QColorDialog::ColorDialogOption>();
+}
+
} // namespace kColorPicker
\ No newline at end of file
diff --git a/src/buttons/ColorDialogButton.h b/src/buttons/ColorDialogButton.h
index 945b242..9b481d2 100644
--- a/src/buttons/ColorDialogButton.h
+++ b/src/buttons/ColorDialogButton.h
@@ -30,11 +30,14 @@ class ColorDialogButton : public AbstractPopupMenuButton
{
Q_OBJECT
public:
- explicit ColorDialogButton(const QIcon &icon);
+ explicit ColorDialogButton(const QIcon &icon, bool showAlphaChannel);
protected:
void buttonClicked() override;
+private:
+ bool mShowAlphaChannel;
+ QFlags<QColorDialog::ColorDialogOption> getColorDialogOptions() const;
};
} // namespace kColorPicker
More information about the Neon-commits
mailing list