color_button.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * color_button.cpp - choose color button
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <QColorDialog>
  19. #include <QDebug>
  20. #include <fmt/format.h>
  21. #include <core/color.h>
  22. #include "color_button.h"
  23. using namespace fmt::literals;
  24. namespace studio {
  25. ColorButton::ColorButton(QWidget* parent) :
  26. QPushButton(parent)
  27. {
  28. connect(this, SIGNAL(clicked(bool)), this, SLOT(choose_color()));
  29. }
  30. void ColorButton::setReadOnly(bool ro) {
  31. setEnabled(!ro);
  32. }
  33. void ColorButton::choose_color() {
  34. auto qcolor = QColor(color.r, color.g, color.b, color.a);
  35. auto selected = QColorDialog::getColor(qcolor, nullptr, "", QColorDialog::ShowAlphaChannel);
  36. update_value({selected.red(), selected.green(), selected.blue(), selected.alpha()});
  37. update_button_color();
  38. Q_EMIT editingFinished();
  39. }
  40. void ColorButton::update_button_color() {
  41. auto c = core::colors::to_hex24(color);
  42. // TODO: keep colour when focused (perhaps use icon instead of style?)
  43. auto style = QString::fromStdString("background-color: {}"_format(c));
  44. setStyleSheet(style);
  45. }
  46. void ColorButton::update_value(core::colors::Color color_) {
  47. color = color_;
  48. update_button_color();
  49. }
  50. } // namespace studio