Update upstream source from tag 'upstream/0.2.0'
Update to upstream version '0.2.0'
with Debian dir eda19a65b1eb0b08549f629be770a51d75192412
Boyuan Yang
1 year, 11 months ago
0 | 0 | # These are supported funding model platforms |
1 | 1 | |
2 | github: [DamirPorobic] | |
2 | github: DamirPorobic | |
3 | liberapay: dporobic | |
4 | patreon: dporobic | |
5 | open_collective: ksnip | |
3 | 6 | custom: paypal.me/damirporobic |
0 | name: Build | |
1 | ||
2 | on: | |
3 | push: | |
4 | branches: [ master ] | |
5 | pull_request: | |
6 | branches: [ master ] | |
7 | ||
8 | jobs: | |
9 | build: | |
10 | runs-on: ubuntu-latest | |
11 | ||
12 | steps: | |
13 | - uses: actions/checkout@v2 | |
14 | ||
15 | - name: Install Qt | |
16 | uses: jurplel/install-qt-action@v2 | |
17 | with: | |
18 | version: '5.15.2' | |
19 | host: 'linux' | |
20 | install-deps: 'true' | |
21 | ||
22 | - name: Install dependencies | |
23 | run: sudo apt-get install xvfb | |
24 | ||
25 | - name: Build | |
26 | run: | | |
27 | mkdir build && cd build | |
28 | cmake .. -DBUILD_TESTS=ON && make VERBOSE=1 | |
29 | ||
30 | - name: Test | |
31 | working-directory: ${{github.workspace}}/build | |
32 | run: xvfb-run make test CTEST_OUTPUT_ON_FAILURE=1 |
0 | language: cpp | |
1 | compiler: gcc | |
2 | sudo: required | |
3 | dist: trusty | |
4 | ||
5 | before_install: | |
6 | - sudo add-apt-repository ppa:beineri/opt-qt594-trusty -y | |
7 | - sudo apt-get update -qq | |
8 | ||
9 | install: | |
10 | - sudo apt-get -y install qt59base | |
11 | - source /opt/qt*/bin/qt*-env.sh | |
12 | ||
13 | jobs: | |
14 | include: | |
15 | - stage: build | |
16 | script: | |
17 | - mkdir build | |
18 | - cd build | |
19 | - cmake .. | |
20 | - make VERBOSE=1 | |
21 | - stage: test | |
22 | script: | |
23 | - mkdir build | |
24 | - cd build | |
25 | - cmake .. -DBUILD_TESTS=ON | |
26 | - make | |
27 | - xvfb-run make test CTEST_OUTPUT_ON_FAILURE=1 |
0 | 0 | cmake_minimum_required(VERSION 3.5) |
1 | project(kColorPicker LANGUAGES CXX VERSION 0.1.6) | |
1 | project(kColorPicker LANGUAGES CXX VERSION 0.2.0) | |
2 | 2 | |
3 | 3 | set(QT_MIN_VERSION "5.9.4") |
4 | 4 |
0 | # kColorPicker [![Build Status](https://travis-ci.org/ksnip/kColorPicker.svg?branch=master)](https://travis-ci.org/ksnip/kColorPicker) | |
0 | # kColorPicker [![Build Status][github-badge]][github-url] | |
1 | 1 | 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. |
2 | 2 | |
3 | Version 0.1.6 | |
3 | Version 0.2.0 | |
4 | 4 | |
5 | 5 | ![kColorPicker](https://i.imgur.com/VkhUvFa.png "kColorPicker") |
6 | 6 | |
7 | 7 | ### Building from source |
8 | 1. Get latest release from GitHub by cloning the repo: | |
8 | 1. Get the latest release from GitHub by cloning the repo: | |
9 | 9 | `$ git clone https://github.com/ksnip/kColorPicker` |
10 | 10 | 2. Change to repo directory: |
11 | 11 | `$ cd kColorPicker` |
33 | 33 | |
34 | 34 | 2. Link the library with your application |
35 | 35 | `target_link_libraries(myApp kColorPicker)` |
36 | ||
37 | ||
38 | [github-badge]: https://github.com/ksnip/kColorPicker/actions/workflows/build.yml/badge.svg | |
39 | [github-url]: https://github.com/ksnip/kColorPicker/actions⏎ |
29 | 29 | widget->setFixedWidth(200); |
30 | 30 | widget->setFixedHeight(200); |
31 | 31 | auto layout = new QVBoxLayout(); |
32 | auto colorPicker = new KColorPicker(); | |
32 | auto colorPicker = new KColorPicker(true); | |
33 | 33 | colorPicker->setColor(QColor(Qt::red)); |
34 | 34 | layout->addWidget(colorPicker); |
35 | 35 | widget->setLayout(layout); |
36 | 36 | Q_DECLARE_PRIVATE(KColorPicker) |
37 | 37 | |
38 | 38 | public: |
39 | explicit KColorPicker(); | |
39 | explicit KColorPicker(bool showAlphaChannel = false, QWidget *parent = nullptr); | |
40 | 40 | ~KColorPicker() override; |
41 | 41 | void setFixedSize(const QSize &size); |
42 | 42 | void setFixedSize(int width, int height); |
43 | 43 | QColor color() const; |
44 | void resetColors(bool showAlphaChannel = false); | |
44 | 45 | |
45 | 46 | public Q_SLOTS: |
46 | 47 | void setColor(const QColor &color); |
22 | 22 | |
23 | 23 | QIcon IconCreator::createIcon(const QColor &color, const QSize &size) |
24 | 24 | { |
25 | QPixmap pixmap(size); | |
26 | pixmap.fill(color); | |
27 | QPainter painter(&pixmap); | |
25 | auto background = getBackgroundBrush(size); | |
26 | ||
27 | QPixmap pixmap(size); | |
28 | QPainter painter(&pixmap); | |
29 | ||
30 | // Paint background | |
31 | painter.setPen(Qt::NoPen); | |
32 | painter.setBrush(background); | |
33 | painter.drawRect(0, 0, size.width(), size.height()); | |
34 | ||
35 | // Paint color | |
36 | painter.setBrush(color); | |
37 | painter.drawRect(0, 0, size.width(), size.height()); | |
38 | ||
39 | // Paint border | |
28 | 40 | auto penWidth = painter.pen().width(); |
29 | 41 | painter.setPen(QColor(Qt::gray)); |
30 | 42 | painter.drawRect(0, 0, size.width() - penWidth, size.height() - penWidth); |
31 | 43 | |
32 | return QIcon(pixmap); | |
44 | return { pixmap }; | |
45 | } | |
46 | ||
47 | QImage IconCreator::getBackgroundBrush(const QSize &size) | |
48 | { | |
49 | auto halfWidth = size.width() / 2; | |
50 | auto halfHeight = size.height() / 2; | |
51 | auto background = QImage(size, QImage::Format_ARGB32_Premultiplied); | |
52 | background.fill(Qt::white); | |
53 | QPainter painter(&background); | |
54 | painter.setPen(Qt::NoPen); | |
55 | painter.setBrush(Qt::gray); | |
56 | painter.drawRect(0, 0, halfWidth, halfHeight); | |
57 | painter.drawRect(halfWidth, halfHeight, size.width(), size.height()); | |
58 | ||
59 | return background; | |
33 | 60 | } |
34 | 61 | |
35 | 62 | } // namespace kColorPicker⏎ |
22 | 22 | #include <QIcon> |
23 | 23 | #include <QColor> |
24 | 24 | #include <QPainter> |
25 | #include <QImage> | |
25 | 26 | |
26 | 27 | namespace kColorPicker { |
27 | 28 | |
31 | 32 | explicit IconCreator() = delete; |
32 | 33 | ~IconCreator() = delete; |
33 | 34 | static QIcon createIcon(const QColor &color, const QSize &size); |
35 | static QImage getBackgroundBrush(const QSize &size); | |
34 | 36 | }; |
35 | 37 | |
36 | 38 | } // namespace kColorPicker |
34 | 34 | |
35 | 35 | Q_DECLARE_PUBLIC(KColorPicker) |
36 | 36 | |
37 | explicit KColorPickerPrivate(KColorPicker *kColorPicker); | |
37 | explicit KColorPickerPrivate(KColorPicker *kColorPicker, bool showAlphaChannel = false, QWidget *parent = nullptr); | |
38 | 38 | |
39 | 39 | KColorPicker *const q_ptr; |
40 | 40 | QSize mIconSize; |
41 | PopupMenu mPopupMenu; | |
41 | PopupMenu *mPopupMenu; | |
42 | 42 | QColor mSelectedColor; |
43 | 43 | }; |
44 | 44 | |
45 | KColorPicker::KColorPicker() : d_ptr(new KColorPickerPrivate(this)) | |
46 | {} | |
45 | KColorPicker::KColorPicker(bool showAlphaChannel, QWidget *parent) : d_ptr(new KColorPickerPrivate(this, showAlphaChannel, parent)) | |
46 | { | |
47 | // Default Colors | |
48 | resetColors(showAlphaChannel); | |
49 | } | |
47 | 50 | |
48 | 51 | KColorPicker::~KColorPicker() |
49 | 52 | {} |
53 | 56 | Q_D(KColorPicker); |
54 | 57 | d->mSelectedColor = color; |
55 | 58 | setColorIcon(color); |
56 | d->mPopupMenu.selectColor(color); | |
59 | d->mPopupMenu->selectColor(color); | |
57 | 60 | } |
58 | 61 | |
59 | 62 | void KColorPicker::colorSelected(const QColor &color) |
86 | 89 | return d->mSelectedColor; |
87 | 90 | } |
88 | 91 | |
92 | void KColorPicker::resetColors(bool showAlphaChannel) | |
93 | { | |
94 | Q_D(const KColorPicker); | |
95 | d->mPopupMenu->removeColors(); | |
96 | ||
97 | d->mPopupMenu->addColor(QColor(Qt::red)); | |
98 | d->mPopupMenu->addColor(QColor(Qt::green)); | |
99 | d->mPopupMenu->addColor(QColor(Qt::blue)); | |
100 | d->mPopupMenu->addColor(QColor(Qt::yellow)); | |
101 | d->mPopupMenu->addColor(QColor(Qt::magenta)); | |
102 | d->mPopupMenu->addColor(QColor(Qt::cyan)); | |
103 | d->mPopupMenu->addColor(QColor(Qt::white)); | |
104 | d->mPopupMenu->addColor(QColor(Qt::black)); | |
105 | ||
106 | if(showAlphaChannel) { | |
107 | d->mPopupMenu->addColor(QColor(0, 255, 255, 100)); | |
108 | d->mPopupMenu->addColor(QColor(255, 0, 255, 100)); | |
109 | d->mPopupMenu->addColor(QColor(255, 255, 0, 100)); | |
110 | d->mPopupMenu->addColor(QColor(255, 255, 255, 100)); | |
111 | } | |
112 | } | |
113 | ||
89 | 114 | void KColorPicker::setIconSize(const QSize &size) |
90 | 115 | { |
91 | 116 | Q_D(KColorPicker); |
106 | 131 | // KColorPickerPrivate |
107 | 132 | // |
108 | 133 | |
109 | KColorPickerPrivate::KColorPickerPrivate(KColorPicker *kColorPicker) : q_ptr(kColorPicker) | |
134 | KColorPickerPrivate::KColorPickerPrivate(KColorPicker *kColorPicker, bool showAlphaChannel, QWidget *parent) : | |
135 | q_ptr(kColorPicker), | |
136 | mPopupMenu(new PopupMenu(showAlphaChannel, parent)) | |
110 | 137 | { |
111 | 138 | initResource(); |
112 | 139 | |
113 | 140 | mIconSize = QSize(25, 25); |
114 | 141 | kColorPicker->setPopupMode(QToolButton::InstantPopup); |
115 | kColorPicker->setMenu(&mPopupMenu); | |
116 | kColorPicker->connect(&mPopupMenu, &PopupMenu::colorChanged, kColorPicker, &KColorPicker::colorSelected); | |
117 | ||
118 | // Default Colors | |
119 | mPopupMenu.addColor(QColor(Qt::red)); | |
120 | mPopupMenu.addColor(QColor(Qt::green)); | |
121 | mPopupMenu.addColor(QColor(Qt::blue)); | |
122 | mPopupMenu.addColor(QColor(Qt::yellow)); | |
123 | mPopupMenu.addColor(QColor(Qt::magenta)); | |
124 | mPopupMenu.addColor(QColor(Qt::cyan)); | |
125 | mPopupMenu.addColor(QColor(Qt::white)); | |
126 | mPopupMenu.addColor(QColor(Qt::black)); | |
127 | ||
142 | kColorPicker->setMenu(mPopupMenu); | |
143 | kColorPicker->connect(mPopupMenu, &PopupMenu::colorChanged, kColorPicker, &KColorPicker::colorSelected); | |
128 | 144 | } |
129 | 145 | |
130 | 146 | } // namespace kColorPicker |
20 | 20 | |
21 | 21 | namespace kColorPicker { |
22 | 22 | |
23 | PopupMenu::PopupMenu() : | |
24 | mButtonGroup(new QButtonGroup()), | |
25 | mLayout(new QGridLayout()), | |
26 | mColorDialogButton(new ColorDialogButton(QIcon(QLatin1String(":/icons/ellipsis")))) | |
23 | PopupMenu::PopupMenu(bool showAlphaChannel, QWidget *parent) : | |
24 | QMenu(parent), | |
25 | mButtonGroup(new QButtonGroup(this)), | |
26 | mLayout(new QGridLayout(this)), | |
27 | mColorDialogButton(new ColorDialogButton(QIcon(QLatin1String(":/icons/ellipsis")), showAlphaChannel)) | |
27 | 28 | { |
28 | ||
29 | 29 | mLayout->setSpacing(0); |
30 | 30 | mLayout->setMargin(5); |
31 | 31 | setLayout(mLayout); |
35 | 35 | |
36 | 36 | PopupMenu::~PopupMenu() |
37 | 37 | { |
38 | delete mButtonGroup; | |
38 | qDeleteAll(mColorButtons); | |
39 | 39 | delete mColorDialogButton; |
40 | 40 | } |
41 | 41 | |
44 | 44 | if (!isColorInGrid(color)) { |
45 | 45 | addColorButton(color); |
46 | 46 | } |
47 | } | |
48 | ||
49 | void PopupMenu::removeColors() | |
50 | { | |
51 | for (auto button : mColorButtons) { | |
52 | mButtonGroup->removeButton(button); | |
53 | mLayout->removeWidget(button); | |
54 | disconnect(button, &AbstractPopupMenuButton::colorSelected, this, &PopupMenu::colorSelected); | |
55 | } | |
56 | ||
57 | qDeleteAll(mColorButtons); | |
58 | mColorButtons.clear(); | |
59 | ||
60 | generateGrid(); | |
47 | 61 | } |
48 | 62 | |
49 | 63 | void PopupMenu::selectColor(const QColor &color) |
33 | 33 | { |
34 | 34 | Q_OBJECT |
35 | 35 | public: |
36 | PopupMenu(); | |
36 | PopupMenu(bool showAlphaChannel, QWidget *parent); | |
37 | 37 | ~PopupMenu() override; |
38 | 38 | void addColor(const QColor &color); |
39 | void removeColors(); | |
39 | 40 | void selectColor(const QColor &color); |
40 | 41 | QSize sizeHint() const; |
41 | 42 | |
59 | 60 | }; |
60 | 61 | |
61 | 62 | } // namespace kColorPicker |
63 | ||
62 | 64 | #endif //KCOLORPICKER_POPUPMENU_H |
23 | 23 | ColorButton::ColorButton(const QIcon &icon, const QColor &color) : AbstractPopupMenuButton(icon) |
24 | 24 | { |
25 | 25 | setCheckable(true); |
26 | setToolTip(color.name()); | |
26 | setToolTip(getColorName(color)); | |
27 | 27 | mColor = color; |
28 | 28 | } |
29 | 29 | |
37 | 37 | emit colorSelected(mColor); |
38 | 38 | } |
39 | 39 | |
40 | QString ColorButton::getColorName(const QColor &color) | |
41 | { | |
42 | auto format = color.alpha() < 255 ? QColor::HexArgb : QColor::HexRgb; | |
43 | ||
44 | return color.name(format); | |
45 | } | |
46 | ||
40 | 47 | } // namespace kColorPicker⏎ |
35 | 35 | |
36 | 36 | private: |
37 | 37 | QColor mColor; |
38 | ||
39 | static QString getColorName(const QColor &color); | |
38 | 40 | }; |
39 | 41 | |
40 | 42 | } // namespace kColorPicker |
20 | 20 | |
21 | 21 | namespace kColorPicker { |
22 | 22 | |
23 | ColorDialogButton::ColorDialogButton(const QIcon &icon) : AbstractPopupMenuButton(icon) | |
23 | ColorDialogButton::ColorDialogButton(const QIcon &icon, bool showAlphaChannel) : | |
24 | AbstractPopupMenuButton(icon), | |
25 | mShowAlphaChannel(showAlphaChannel) | |
24 | 26 | { |
25 | 27 | setCheckable(false); |
26 | 28 | } |
27 | 29 | |
28 | 30 | void ColorDialogButton::buttonClicked() |
29 | 31 | { |
30 | auto color = QColorDialog::getColor(); | |
32 | auto color = QColorDialog::getColor(Qt::white, this, QString(), getColorDialogOptions()); | |
31 | 33 | emit colorSelected(color); |
32 | 34 | } |
33 | 35 | |
36 | QFlags<QColorDialog::ColorDialogOption> ColorDialogButton::getColorDialogOptions() const | |
37 | { | |
38 | return mShowAlphaChannel ? QColorDialog::ShowAlphaChannel : QFlags<QColorDialog::ColorDialogOption>(); | |
39 | } | |
40 | ||
34 | 41 | } // namespace kColorPicker⏎ |
29 | 29 | { |
30 | 30 | Q_OBJECT |
31 | 31 | public: |
32 | explicit ColorDialogButton(const QIcon &icon); | |
32 | explicit ColorDialogButton(const QIcon &icon, bool showAlphaChannel); | |
33 | 33 | |
34 | 34 | protected: |
35 | 35 | void buttonClicked() override; |
36 | 36 | |
37 | private: | |
38 | bool mShowAlphaChannel; | |
39 | QFlags<QColorDialog::ColorDialogOption> getColorDialogOptions() const; | |
37 | 40 | }; |
38 | 41 | |
39 | 42 | } // namespace kColorPicker |