screenshot.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * \file
  3. *
  4. * \author Mattia Basaglia
  5. *
  6. * \copyright Copyright (C) 2013-2020 Mattia Basaglia
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include <cstring>
  23. #include <algorithm>
  24. #include <QApplication>
  25. #include <QCommandLineParser>
  26. #include <QComboBox>
  27. #include <QListView>
  28. #include <QTableWidget>
  29. #include "QtColorWidgets/color_2d_slider.hpp"
  30. #include "QtColorWidgets/color_delegate.hpp" /// \todo show it
  31. #include "QtColorWidgets/color_dialog.hpp"
  32. #include "QtColorWidgets/color_line_edit.hpp"
  33. #include "QtColorWidgets/color_list_widget.hpp"
  34. #include "QtColorWidgets/color_palette_widget.hpp"
  35. #include "QtColorWidgets/color_preview.hpp"
  36. #include "QtColorWidgets/color_wheel.hpp"
  37. #include "QtColorWidgets/harmony_color_wheel.hpp"
  38. #include "QtColorWidgets/hue_slider.hpp"
  39. #include "QtColorWidgets/gradient_editor.hpp"
  40. #include "QtColorWidgets/gradient_list_model.hpp"
  41. #include "QtColorWidgets/gradient_delegate.hpp"
  42. bool run = false;
  43. QStringList just_these;
  44. void screenshot(QWidget& widget, QString name = QString())
  45. {
  46. if ( name.isEmpty() )
  47. {
  48. name = widget.metaObject()->className();
  49. name.remove("color_widgets::");
  50. }
  51. if ( !just_these.isEmpty() && !just_these.contains(name) )
  52. return;
  53. widget.setWindowTitle(name);
  54. QPixmap pic(widget.size());
  55. widget.render(&pic);
  56. name += ".png";
  57. pic.save(name);
  58. if ( run )
  59. widget.show();
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. QApplication a(argc, argv);
  64. QCommandLineParser parser;
  65. parser.addHelpOption();
  66. parser.addPositionalArgument("just_these", "Only these widgets");
  67. QCommandLineOption run_option("run", "Show widgets instead of saving to file");
  68. parser.addOption(run_option);
  69. parser.process(a);
  70. run = parser.isSet(run_option);
  71. just_these = parser.positionalArguments();
  72. QColor demo_color(64,172,143,128);
  73. color_widgets::ColorPalette palette1;
  74. color_widgets::ColorPalette palette2;
  75. color_widgets::ColorPalette palette3;
  76. int palette_columns = 12;
  77. palette1.setName("Palette 1");
  78. palette2.setName("Palette 2");
  79. palette3.setName("Palette 3");
  80. palette1.setColumns(palette_columns);
  81. palette2.setColumns(palette_columns);
  82. for ( int i = 0; i < 6; i++ )
  83. {
  84. for ( int j = 0; j < palette_columns; j++ )
  85. {
  86. float f = float(j)/palette_columns;
  87. palette1.appendColor(QColor::fromHsvF(i/8.0,1-f,0.5+f/2));
  88. palette2.appendColor(QColor::fromHsvF(i/8.0,1-f,1-f));
  89. }
  90. palette3.appendColor(QColor::fromHsvF(i/8.0, 0.8, 1));
  91. }
  92. color_widgets::ColorPaletteModel palette_model;
  93. palette_model.addPalette(palette1, false);
  94. palette_model.addPalette(palette2, false);
  95. palette_model.addPalette(palette3, false);
  96. color_widgets::ColorPreview preview;
  97. preview.setColor(demo_color);
  98. preview.setDisplayMode(color_widgets::ColorPreview::SplitAlpha);
  99. preview.resize(128,32);
  100. screenshot(preview);
  101. color_widgets::ColorDialog dialog;
  102. dialog.setColorSpace(color_widgets::ColorWheel::ColorLCH);
  103. dialog.setColor(demo_color);
  104. screenshot(dialog);
  105. color_widgets::Color2DSlider slider2d;
  106. slider2d.setColor(demo_color);
  107. slider2d.resize(128,192);
  108. screenshot(slider2d);
  109. color_widgets::ColorLineEdit line_edit;
  110. line_edit.setColor(demo_color);
  111. line_edit.resize(line_edit.sizeHint());
  112. screenshot(line_edit);
  113. line_edit.setPreviewColor(true);
  114. screenshot(line_edit, "ColorLineEdit_with_color");
  115. color_widgets::ColorWheel wheel;
  116. wheel.resize(256, 256);
  117. wheel.setColor(demo_color);
  118. screenshot(wheel);
  119. color_widgets::HarmonyColorWheel harwheel;
  120. harwheel.resize(256, 256);
  121. harwheel.setColor(demo_color);
  122. harwheel.addHarmony(.333, true);
  123. harwheel.addHarmony(.667, true);
  124. screenshot(harwheel);
  125. color_widgets::Swatch swatch;
  126. swatch.setPalette(palette1);
  127. swatch.resize(swatch.sizeHint());
  128. screenshot(swatch);
  129. color_widgets::ColorPaletteWidget palette_widget;
  130. palette_widget.setModel(&palette_model);
  131. screenshot(palette_widget);
  132. color_widgets::ColorPaletteWidget palette_widget1;
  133. palette_widget1.setModel(&palette_model);
  134. palette_widget1.setReadOnly(true);
  135. screenshot(palette_widget1, "ColorPaletteWidget_readonly");
  136. color_widgets::HueSlider hue_slider;
  137. hue_slider.setColor(demo_color);
  138. hue_slider.resize(192, hue_slider.sizeHint().height());
  139. // hue_slider.setInvertedAppearance(true);
  140. // hue_slider.setOrientation(Qt::Vertical);
  141. screenshot(hue_slider);
  142. color_widgets::ColorListWidget list_widget;
  143. list_widget.setColors({
  144. demo_color,
  145. palette1.colorAt(palette_columns*0),
  146. palette1.colorAt(palette_columns*1),
  147. palette1.colorAt(palette_columns*3),
  148. palette1.colorAt(palette_columns*5),
  149. });
  150. list_widget.resize(list_widget.sizeHint());
  151. screenshot(list_widget);
  152. color_widgets::GradientEditor editor;
  153. QGradientStops gradient_colors;
  154. float n_colors = 4;
  155. for ( int i = 0; i <= n_colors; ++i )
  156. gradient_colors.append(QGradientStop(i/n_colors, QColor::fromHsvF(i/n_colors, 0.5, 1)));
  157. editor.setStops(gradient_colors);
  158. screenshot(editor);
  159. QComboBox gradient_list;
  160. color_widgets::GradientListModel gradient_model;
  161. gradient_model.setGradient("Rainbow", gradient_colors);
  162. gradient_model.setGradient("Black to Transparent", QGradientStops{{0, Qt::black}, {1, QColor(0, 0, 0, 0)}});
  163. gradient_list.setModel(&gradient_model);
  164. gradient_model.setIconSize(QSize(128, 24));
  165. gradient_list.setIconSize(gradient_model.iconSize());
  166. QObject::connect(&editor, &color_widgets::GradientEditor::stopsChanged, &gradient_model,
  167. [&gradient_model](const QGradientStops& stops){ gradient_model.setGradient("Rainbow", stops); });
  168. gradient_list.resize(gradient_list.sizeHint());
  169. screenshot(gradient_list, "GradientListModel_combo");
  170. QListView gradient_view;
  171. color_widgets::GradientDelegate gradient_delegate;
  172. gradient_view.setItemDelegate(&gradient_delegate);
  173. gradient_view.setModel(&gradient_model);
  174. // gradient_model.setEditMode(color_widgets::GradientListModel::EditName);
  175. gradient_model.setEditMode(color_widgets::GradientListModel::EditGradient);
  176. gradient_view.resize(QSize(gradient_view.sizeHintForColumn(0) + 4, gradient_view.sizeHint().height()));
  177. screenshot(gradient_view, "GradientListModel_view");
  178. QTableWidget gradient_table;
  179. gradient_table.setItemDelegate(&gradient_delegate);
  180. gradient_table.setRowCount(2);
  181. gradient_table.setColumnCount(2);
  182. gradient_table.setItem(0, 0, new QTableWidgetItem());
  183. gradient_table.item(0, 0)->setData(Qt::EditRole, QVariant::fromValue(gradient_model.gradientBrush(0)));
  184. gradient_table.setItem(0, 1, new QTableWidgetItem(gradient_model.nameFromIndex(0)));
  185. gradient_table.setItem(1, 0, new QTableWidgetItem());
  186. gradient_table.item(1, 0)->setData(Qt::EditRole, QVariant::fromValue(gradient_model.gradientBrush(1)));
  187. gradient_table.setItem(1, 1, new QTableWidgetItem(gradient_model.nameFromIndex(1)));
  188. screenshot(gradient_table, "GradientDelegate_table");
  189. if ( run )
  190. return a.exec();
  191. return 0;
  192. }