color_button.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <util/strings.h>
  23. #include "color_button.h"
  24. using namespace fmt::literals;
  25. namespace rainynite::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({
  38. (uint8_t) selected.red(),
  39. (uint8_t) selected.green(),
  40. (uint8_t) selected.blue(),
  41. (uint8_t) selected.alpha()
  42. });
  43. update_button_color();
  44. Q_EMIT editingFinished();
  45. }
  46. void ColorButton::update_button_color() {
  47. auto c = core::colors::to_hex24(color);
  48. // TODO: keep colour when focused (perhaps use icon instead of style?)
  49. setStyleSheet(util::str("background-color: {}"_format(c)));
  50. }
  51. void ColorButton::update_value(core::colors::Color color_) {
  52. color = color_;
  53. update_button_color();
  54. }
  55. } // namespace rainynite::studio