color_button.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* color_button.cpp - choose color button
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <QColorDialog>
  18. #include <QDebug>
  19. #include <fmt/format.h>
  20. #include <core/color/color.h>
  21. #include <util/strings.h>
  22. #include "color_button.h"
  23. using namespace fmt::literals;
  24. namespace rainynite::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({
  37. (uint8_t) selected.red(),
  38. (uint8_t) selected.green(),
  39. (uint8_t) selected.blue(),
  40. (uint8_t) selected.alpha()
  41. });
  42. update_button_color();
  43. Q_EMIT editingFinished();
  44. }
  45. void ColorButton::update_button_color() {
  46. auto c = core::colors::to_hex24(color);
  47. // TODO: keep colour when focused (perhaps use icon instead of style?)
  48. setStyleSheet(util::str("background-color: {}"_format(c)));
  49. }
  50. void ColorButton::update_value(core::colors::Color color_) {
  51. color = color_;
  52. update_button_color();
  53. }
  54. } // namespace rainynite::studio