color_button.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/node.h>
  22. #include <core/color.h>
  23. #include "color_button.h"
  24. using namespace fmt::literals;
  25. namespace studio {
  26. ColorButton::ColorButton(QWidget* parent) :
  27. QPushButton(parent)
  28. {
  29. connect(this, SIGNAL(clicked(bool)), this, SLOT(choose_color()));
  30. }
  31. void ColorButton::setReadOnly(bool ro) {
  32. setEnabled(!ro);
  33. }
  34. void ColorButton::choose_color() {
  35. auto qcolor = QColor(color.r, color.g, color.b, color.a);
  36. auto selected = QColorDialog::getColor(qcolor, nullptr, "", QColorDialog::ShowAlphaChannel);
  37. update_value({selected.red(), selected.green(), selected.blue(), selected.alpha()});
  38. update_button_color();
  39. Q_EMIT editingFinished();
  40. }
  41. void ColorButton::update_button_color() {
  42. auto c = core::colors::to_hex24(color);
  43. // TODO: keep colour when focused (perhaps use icon instead of style?)
  44. auto style = QString::fromStdString("background-color: {}"_format(c));
  45. setStyleSheet(style);
  46. }
  47. void ColorButton::update_value(core::colors::Color color_) {
  48. color = color_;
  49. update_button_color();
  50. }
  51. } // namespace studio