comparison src/gui/ui_utils.cpp @ 9:5c0397762b53

INCOMPLETE: megacommit :)
author Paper <mrpapersonic@gmail.com>
date Sun, 10 Sep 2023 03:59:16 -0400
parents src/ui_utils.cpp@b1f73678ef61
children cde8f67a7c7d
comparison
equal deleted inserted replaced
8:b1f73678ef61 9:5c0397762b53
1 /**
2 * FIXME: most of these can actually be rerouted to *separate* files.
3 * Please do this! It makes everything cleaner :)
4 **/
5 #include "gui/ui_utils.h"
6 #include "core/session.h"
7 #include <QFrame>
8 #include <QLabel>
9 #include <QPixmap>
10 #include <QTextBlock>
11 #include <QVBoxLayout>
12 #ifdef MACOSX
13 #include "sys/osx/dark_theme.h"
14 #else
15 #include "sys/win32/dark_theme.h"
16 #endif
17
18 namespace UiUtils {
19
20 bool IsInDarkMode() {
21 if (session.config.theme != Themes::OS)
22 return (session.config.theme == Themes::DARK);
23 #ifdef MACOSX
24 if (osx::DarkThemeAvailable()) {
25 if (osx::IsInDarkTheme()) {
26 return true;
27 } else {
28 return false;
29 }
30 }
31 #elif defined(WIN32)
32 if (win32::DarkThemeAvailable()) {
33 if (win32::IsInDarkTheme()) {
34 return true;
35 } else {
36 return false;
37 }
38 }
39 #endif
40 return (session.config.theme == Themes::DARK);
41 }
42
43 Header::Header(QString title, QWidget* parent) : QWidget(parent) {
44 setLayout(new QVBoxLayout);
45 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
46
47 static_text_title = new QLabel(title, this);
48 static_text_title->setTextFormat(Qt::PlainText);
49 QFont font = static_text_title->font();
50 font.setWeight(QFont::Bold);
51 static_text_title->setFont(font);
52 static_text_title->setFixedHeight(16);
53
54 static_text_line = new QFrame(this);
55 static_text_line->setFrameShape(QFrame::HLine);
56 static_text_line->setFrameShadow(QFrame::Sunken);
57 static_text_line->setFixedHeight(2);
58
59 layout()->addWidget(static_text_title);
60 layout()->addWidget(static_text_line);
61 layout()->setSpacing(0);
62 layout()->setMargin(0);
63 }
64
65 void Header::SetTitle(QString title) {
66 static_text_title->setText(title);
67 }
68
69 TextParagraph::TextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) {
70 setLayout(new QVBoxLayout);
71
72 header = new Header(title, this);
73
74 QWidget* content = new QWidget(this);
75 content->setLayout(new QHBoxLayout);
76
77 paragraph = new Paragraph(data, this);
78 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
79 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
80 paragraph->setWordWrapMode(QTextOption::NoWrap);
81
82 content->layout()->addWidget(paragraph);
83 content->layout()->setSpacing(0);
84 content->layout()->setMargin(0);
85 content->setContentsMargins(12, 0, 0, 0);
86
87 layout()->addWidget(header);
88 layout()->addWidget(paragraph);
89 layout()->setSpacing(0);
90 layout()->setMargin(0);
91 }
92
93 Header* TextParagraph::GetHeader() {
94 return header;
95 }
96
97 Paragraph* TextParagraph::GetParagraph() {
98 return paragraph;
99 }
100
101 LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, QString data, QWidget* parent)
102 : QWidget(parent) {
103 setLayout(new QVBoxLayout);
104
105 header = new Header(title, this);
106
107 // this is not accessible from the object because there's really
108 // no reason to make it accessible...
109 QWidget* content = new QWidget(this);
110 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
111
112 labels = new Paragraph(label, this);
113 labels->setTextInteractionFlags(Qt::NoTextInteraction);
114 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
115 labels->setWordWrapMode(QTextOption::NoWrap);
116 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
117 labels->setFixedWidth(123);
118
119 paragraph = new Paragraph(data, this);
120 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
121 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
122 paragraph->setWordWrapMode(QTextOption::NoWrap);
123
124 QHBoxLayout* content_layout = new QHBoxLayout;
125 content_layout->addWidget(labels, 0, Qt::AlignTop);
126 content_layout->addWidget(paragraph, 0, Qt::AlignTop);
127 content_layout->setSpacing(0);
128 content_layout->setMargin(0);
129 content->setLayout(content_layout);
130
131 content->setContentsMargins(12, 0, 0, 0);
132
133 layout()->addWidget(header);
134 layout()->addWidget(content);
135 layout()->setSpacing(0);
136 layout()->setMargin(0);
137 }
138
139 Header* LabelledTextParagraph::GetHeader() {
140 return header;
141 }
142
143 Paragraph* LabelledTextParagraph::GetLabels() {
144 return labels;
145 }
146
147 Paragraph* LabelledTextParagraph::GetParagraph() {
148 return paragraph;
149 }
150
151 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) {
152 setLayout(new QVBoxLayout);
153
154 header = new Header(title, this);
155
156 QWidget* content = new QWidget(this);
157 content->setLayout(new QHBoxLayout);
158
159 paragraph = new Paragraph(data, content);
160
161 content->layout()->addWidget(paragraph);
162 content->layout()->setSpacing(0);
163 content->layout()->setMargin(0);
164 content->setContentsMargins(12, 0, 0, 0);
165
166 layout()->addWidget(header);
167 layout()->addWidget(content);
168 layout()->setSpacing(0);
169 layout()->setMargin(0);
170 }
171
172 Header* SelectableTextParagraph::GetHeader() {
173 return header;
174 }
175
176 Paragraph* SelectableTextParagraph::GetParagraph() {
177 return paragraph;
178 }
179
180 void SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
181 QTextDocument* document = new QTextDocument(text_edit);
182 document->setDocumentLayout(new QPlainTextDocumentLayout(document));
183 document->setPlainText(data);
184 text_edit->setDocument(document);
185 }
186
187 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */
188 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) {
189 setReadOnly(true);
190 setTextInteractionFlags(Qt::TextBrowserInteraction);
191 setFrameShape(QFrame::NoFrame);
192 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
193 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
194 setStyleSheet("background: transparent;");
195 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
196 }
197
198 /* highly based upon... some stackoverflow answer for PyQt */
199 QSize Paragraph::minimumSizeHint() const {
200 QTextDocument* doc = document();
201 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() +
202 (2 * doc->documentMargin()));
203 return QSize(QPlainTextEdit::sizeHint().width(), (long)h);
204 }
205
206 QSize Paragraph::sizeHint() const {
207 return minimumSizeHint();
208 }
209
210 } // namespace UiUtils
211
212 #include "gui/moc_ui_utils.cpp"