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