annotate src/ui_utils.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 1d82f6e04d7d
children b1f73678ef61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
1 #include <QPixmap>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
2 #include <QLabel>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
3 #include <QFrame>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
4 #include <QVBoxLayout>
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
5 #include <QTextBlock>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
6 #include "window.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7 #include "ui_utils.h"
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
8 #include "session.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 #ifdef MACOSX
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 #include "sys/osx/dark_theme.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
11 #else
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
12 #include "sys/win32/dark_theme.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
13 #endif
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
14
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
15 QIcon UiUtils::CreateSideBarIcon(const char* file) {
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
16 QPixmap pixmap(file, "PNG");
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
17 QIcon result;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
18 result.addPixmap(pixmap, QIcon::Normal);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
19 result.addPixmap(pixmap, QIcon::Selected);
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
20 return result;
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
21 }
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
22
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23 bool UiUtils::IsInDarkMode() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24 if (session.config.theme != OS)
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 return (session.config.theme == DARK);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 #ifdef MACOSX
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27 if (osx::DarkThemeAvailable()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 if (osx::IsInDarkTheme()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29 return true;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30 } else {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
31 return false;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
32 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
33 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
34 #elif defined(WIN32)
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
35 if (win32::DarkThemeAvailable()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
36 if (win32::IsInDarkTheme()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
37 return true;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
38 } else {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39 return false;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
40 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 #endif
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 return (session.config.theme == DARK);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
46 void UiUtils::CreateTextHeader(QWidget* parent, QString title) {
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 QLabel* static_text_title = new QLabel(title, parent);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 static_text_title->setTextFormat(Qt::PlainText);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
49
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
50 QFont font = static_text_title->font();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
51 font.setWeight(QFont::Bold);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
52 static_text_title->setFont(font);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
53
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
54 static_text_title->setFixedHeight(16);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
55 parent->layout()->addWidget(static_text_title);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 QFrame* static_text_line = new QFrame(parent);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 static_text_line->setFrameShape(QFrame::HLine);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 static_text_line->setFrameShadow(QFrame::Sunken);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
60 static_text_line->setFixedHeight(2);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
61 parent->layout()->addWidget(static_text_line);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
63
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
64 QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
65 QWidget* paragraph_master = new QWidget(parent);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
66 paragraph_master->setLayout(new QVBoxLayout);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
67
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
68 CreateTextHeader(paragraph_master, title);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
69
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
70 Paragraph* paragraph = new Paragraph(data, paragraph_master);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
71 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
72 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 paragraph->setWordWrapMode(QTextOption::NoWrap);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
74 paragraph->setContentsMargins(12, 0, 0, 0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
75
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
76 paragraph_master->layout()->addWidget(paragraph);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
77 paragraph_master->layout()->setSpacing(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
78 paragraph_master->layout()->setMargin(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
79 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
80
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
81 return paragraph;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
82 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
83
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
84 QPlainTextEdit* UiUtils::CreateTextParagraphWithLabels(QWidget* parent, QString title, QString label, QString data) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
85 QWidget* paragraph_master = new QWidget(parent);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
86 paragraph_master->setLayout(new QVBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
87
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
88 CreateTextHeader(paragraph_master, title);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
89
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
90 QWidget* paragraph_and_label = new QWidget(paragraph_master);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
91 paragraph_and_label->setLayout(new QHBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
92 paragraph_and_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
93
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
94 Paragraph* label_t = new Paragraph(label, paragraph_and_label);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
95 label_t->setTextInteractionFlags(Qt::NoTextInteraction);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
96 label_t->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
97 label_t->setWordWrapMode(QTextOption::NoWrap);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
98 label_t->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
99 label_t->setFixedWidth(123);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
100
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
101 Paragraph* paragraph = new Paragraph(data, paragraph_and_label);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
102 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
103 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
104 paragraph->setWordWrapMode(QTextOption::NoWrap);
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
105
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
106 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(label_t, 0, Qt::AlignTop);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
107 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(paragraph, 0, Qt::AlignTop);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
108
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
109 paragraph_and_label->setContentsMargins(12, 0, 0, 0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
110
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
111 paragraph_master->layout()->addWidget(paragraph_and_label);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
112 paragraph_master->layout()->setSpacing(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
113 paragraph_master->layout()->setMargin(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
114 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
115
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
116 return paragraph;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
117 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
118
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
119 /* As far as I can tell, this is identical to the way Taiga implements it.
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
120 Kind of cool, I didn't even look into the source code for it :p */
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
121 QPlainTextEdit* UiUtils::CreateSelectableTextParagraph(QWidget* parent, QString title, QString data) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
122 QWidget* paragraph_master = new QWidget(parent);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
123 paragraph_master->setLayout(new QVBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
124
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
125 CreateTextHeader(paragraph_master, title);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
126
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
127 QWidget* paragraph_widget = new QWidget(paragraph_master);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
128 paragraph_widget->setLayout(new QHBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
129 paragraph_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
130
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
131 Paragraph* text_edit = new Paragraph(data, paragraph_widget);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
132
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
133 paragraph_widget->layout()->addWidget(text_edit);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
134
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
135 paragraph_widget->setContentsMargins(12, 0, 0, 0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
136
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
137 paragraph_master->layout()->addWidget(paragraph_widget);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
138 paragraph_master->layout()->setSpacing(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
139 paragraph_master->layout()->setMargin(0);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
140
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
141 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
142
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
143 return text_edit;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
144 }
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
145
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
146 void UiUtils::SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
147 QTextDocument* document = new QTextDocument(text_edit);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
148 document->setDocumentLayout(new QPlainTextDocumentLayout(document));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
149 document->setPlainText(data);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
150 text_edit->setDocument(document);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
151 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
152
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
153 Paragraph::Paragraph(QString text, QWidget* parent)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
154 : QPlainTextEdit(text, parent) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
155 setReadOnly(true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
156 setTextInteractionFlags(Qt::TextBrowserInteraction);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
157 setFrameShape(QFrame::NoFrame);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
158 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
159 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
160 setStyleSheet("background: transparent;");
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
161 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
162 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
163
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
164 QSize Paragraph::minimumSizeHint() const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
165 QTextDocument* doc = document();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
166 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin()));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
167 return QSize(QPlainTextEdit::sizeHint().width(), (long)h);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
168 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
169
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
170 QSize Paragraph::sizeHint() const {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
171 return minimumSizeHint();
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
172 }