comparison 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
comparison
equal deleted inserted replaced
7:07a9095eaeed 8:b1f73678ef61
10 #include "sys/osx/dark_theme.h" 10 #include "sys/osx/dark_theme.h"
11 #else 11 #else
12 #include "sys/win32/dark_theme.h" 12 #include "sys/win32/dark_theme.h"
13 #endif 13 #endif
14 14
15 QIcon UiUtils::CreateSideBarIcon(const char* file) { 15 namespace UiUtils {
16
17 QIcon CreateSideBarIcon(const char* file) {
16 QPixmap pixmap(file, "PNG"); 18 QPixmap pixmap(file, "PNG");
17 QIcon result; 19 QIcon result;
18 result.addPixmap(pixmap, QIcon::Normal); 20 result.addPixmap(pixmap, QIcon::Normal);
19 result.addPixmap(pixmap, QIcon::Selected); 21 result.addPixmap(pixmap, QIcon::Selected);
20 return result; 22 return result;
21 } 23 }
22 24
23 bool UiUtils::IsInDarkMode() { 25 bool IsInDarkMode() {
24 if (session.config.theme != OS) 26 if (session.config.theme != OS)
25 return (session.config.theme == DARK); 27 return (session.config.theme == DARK);
26 #ifdef MACOSX 28 #ifdef MACOSX
27 if (osx::DarkThemeAvailable()) { 29 if (osx::DarkThemeAvailable()) {
28 if (osx::IsInDarkTheme()) { 30 if (osx::IsInDarkTheme()) {
41 } 43 }
42 #endif 44 #endif
43 return (session.config.theme == DARK); 45 return (session.config.theme == DARK);
44 } 46 }
45 47
46 void UiUtils::CreateTextHeader(QWidget* parent, QString title) { 48 Header::Header(QString title, QWidget* parent)
47 QLabel* static_text_title = new QLabel(title, parent); 49 : QWidget(parent) {
50 setLayout(new QVBoxLayout);
51 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
52
53 static_text_title = new QLabel(title, this);
48 static_text_title->setTextFormat(Qt::PlainText); 54 static_text_title->setTextFormat(Qt::PlainText);
49
50 QFont font = static_text_title->font(); 55 QFont font = static_text_title->font();
51 font.setWeight(QFont::Bold); 56 font.setWeight(QFont::Bold);
52 static_text_title->setFont(font); 57 static_text_title->setFont(font);
53
54 static_text_title->setFixedHeight(16); 58 static_text_title->setFixedHeight(16);
55 parent->layout()->addWidget(static_text_title); 59
56 60 static_text_line = new QFrame(this);
57 QFrame* static_text_line = new QFrame(parent);
58 static_text_line->setFrameShape(QFrame::HLine); 61 static_text_line->setFrameShape(QFrame::HLine);
59 static_text_line->setFrameShadow(QFrame::Sunken); 62 static_text_line->setFrameShadow(QFrame::Sunken);
60 static_text_line->setFixedHeight(2); 63 static_text_line->setFixedHeight(2);
61 parent->layout()->addWidget(static_text_line); 64
62 } 65 layout()->addWidget(static_text_title);
63 66 layout()->addWidget(static_text_line);
64 QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data) { 67 layout()->setSpacing(0);
65 QWidget* paragraph_master = new QWidget(parent); 68 layout()->setMargin(0);
66 paragraph_master->setLayout(new QVBoxLayout); 69 }
67 70
68 CreateTextHeader(paragraph_master, title); 71 void Header::SetTitle(QString title) {
69 72 static_text_title->setText(title);
70 Paragraph* paragraph = new Paragraph(data, paragraph_master); 73 }
74
75 TextParagraph::TextParagraph(QString title, QString data, QWidget* parent)
76 : QWidget(parent) {
77 setLayout(new QVBoxLayout);
78
79 header = new Header(title, this);
80
81 QWidget* content = new QWidget(this);
82 content->setLayout(new QHBoxLayout);
83
84 paragraph = new Paragraph(data, this);
71 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); 85 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
72 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); 86 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
73 paragraph->setWordWrapMode(QTextOption::NoWrap); 87 paragraph->setWordWrapMode(QTextOption::NoWrap);
74 paragraph->setContentsMargins(12, 0, 0, 0); 88
75 89 content->layout()->addWidget(paragraph);
76 paragraph_master->layout()->addWidget(paragraph); 90 content->layout()->setSpacing(0);
77 paragraph_master->layout()->setSpacing(0); 91 content->layout()->setMargin(0);
78 paragraph_master->layout()->setMargin(0); 92 content->setContentsMargins(12, 0, 0, 0);
79 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 93
80 94 layout()->addWidget(header);
95 layout()->addWidget(paragraph);
96 layout()->setSpacing(0);
97 layout()->setMargin(0);
98 }
99
100 Header* TextParagraph::GetHeader() {
101 return header;
102 }
103
104 Paragraph* TextParagraph::GetParagraph() {
81 return paragraph; 105 return paragraph;
82 } 106 }
83 107
84 QPlainTextEdit* UiUtils::CreateTextParagraphWithLabels(QWidget* parent, QString title, QString label, QString data) { 108 LabelledTextParagraph::LabelledTextParagraph(QString title, QString label, QString data, QWidget* parent)
85 QWidget* paragraph_master = new QWidget(parent); 109 : QWidget(parent) {
86 paragraph_master->setLayout(new QVBoxLayout); 110 setLayout(new QVBoxLayout);
87 111
88 CreateTextHeader(paragraph_master, title); 112 header = new Header(title, this);
89 113
90 QWidget* paragraph_and_label = new QWidget(paragraph_master); 114 // this is not accessible from the object because there's really
91 paragraph_and_label->setLayout(new QHBoxLayout); 115 // no reason to make it accessible...
92 paragraph_and_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 116 QWidget* content = new QWidget(this);
93 117 content->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
94 Paragraph* label_t = new Paragraph(label, paragraph_and_label); 118
95 label_t->setTextInteractionFlags(Qt::NoTextInteraction); 119 labels = new Paragraph(label, this);
96 label_t->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); 120 labels->setTextInteractionFlags(Qt::NoTextInteraction);
97 label_t->setWordWrapMode(QTextOption::NoWrap); 121 labels->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
98 label_t->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 122 labels->setWordWrapMode(QTextOption::NoWrap);
99 label_t->setFixedWidth(123); 123 labels->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
100 124 labels->setFixedWidth(123);
101 Paragraph* paragraph = new Paragraph(data, paragraph_and_label); 125
126 paragraph = new Paragraph(data, this);
102 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); 127 paragraph->setTextInteractionFlags(Qt::NoTextInteraction);
103 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); 128 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
104 paragraph->setWordWrapMode(QTextOption::NoWrap); 129 paragraph->setWordWrapMode(QTextOption::NoWrap);
105 130
106 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(label_t, 0, Qt::AlignTop); 131 QHBoxLayout* content_layout = new QHBoxLayout;
107 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(paragraph, 0, Qt::AlignTop); 132 content_layout->addWidget(labels, 0, Qt::AlignTop);
108 133 content_layout->addWidget(paragraph, 0, Qt::AlignTop);
109 paragraph_and_label->setContentsMargins(12, 0, 0, 0); 134 content_layout->setSpacing(0);
110 135 content_layout->setMargin(0);
111 paragraph_master->layout()->addWidget(paragraph_and_label); 136 content->setLayout(content_layout);
112 paragraph_master->layout()->setSpacing(0); 137
113 paragraph_master->layout()->setMargin(0); 138 content->setContentsMargins(12, 0, 0, 0);
114 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 139
115 140 layout()->addWidget(header);
141 layout()->addWidget(content);
142 layout()->setSpacing(0);
143 layout()->setMargin(0);
144 }
145
146 Header* LabelledTextParagraph::GetHeader() {
147 return header;
148 }
149
150 Paragraph* LabelledTextParagraph::GetLabels() {
151 return labels;
152 }
153
154 Paragraph* LabelledTextParagraph::GetParagraph() {
116 return paragraph; 155 return paragraph;
117 } 156 }
118 157
119 /* As far as I can tell, this is identical to the way Taiga implements it. 158 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent)
120 Kind of cool, I didn't even look into the source code for it :p */ 159 : QWidget(parent) {
121 QPlainTextEdit* UiUtils::CreateSelectableTextParagraph(QWidget* parent, QString title, QString data) { 160 setLayout(new QVBoxLayout);
122 QWidget* paragraph_master = new QWidget(parent); 161
123 paragraph_master->setLayout(new QVBoxLayout); 162 header = new Header(title, this);
124 163
125 CreateTextHeader(paragraph_master, title); 164 QWidget* content = new QWidget(this);
126 165 content->setLayout(new QHBoxLayout);
127 QWidget* paragraph_widget = new QWidget(paragraph_master); 166
128 paragraph_widget->setLayout(new QHBoxLayout); 167 paragraph = new Paragraph(data, content);
129 paragraph_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 168
130 169 content->layout()->addWidget(paragraph);
131 Paragraph* text_edit = new Paragraph(data, paragraph_widget); 170 content->layout()->setSpacing(0);
132 171 content->layout()->setMargin(0);
133 paragraph_widget->layout()->addWidget(text_edit); 172 content->setContentsMargins(12, 0, 0, 0);
134 173
135 paragraph_widget->setContentsMargins(12, 0, 0, 0); 174 layout()->addWidget(header);
136 175 layout()->addWidget(content);
137 paragraph_master->layout()->addWidget(paragraph_widget); 176 layout()->setSpacing(0);
138 paragraph_master->layout()->setSpacing(0); 177 layout()->setMargin(0);
139 paragraph_master->layout()->setMargin(0); 178 }
140 179
141 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); 180 Header* SelectableTextParagraph::GetHeader() {
142 181 return header;
143 return text_edit; 182 }
144 } 183
145 184 Paragraph* SelectableTextParagraph::GetParagraph() {
146 void UiUtils::SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) { 185 return paragraph;
186 }
187
188 void SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) {
147 QTextDocument* document = new QTextDocument(text_edit); 189 QTextDocument* document = new QTextDocument(text_edit);
148 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); 190 document->setDocumentLayout(new QPlainTextDocumentLayout(document));
149 document->setPlainText(data); 191 document->setPlainText(data);
150 text_edit->setDocument(document); 192 text_edit->setDocument(document);
151 } 193 }
152 194
195 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */
153 Paragraph::Paragraph(QString text, QWidget* parent) 196 Paragraph::Paragraph(QString text, QWidget* parent)
154 : QPlainTextEdit(text, parent) { 197 : QPlainTextEdit(text, parent) {
155 setReadOnly(true); 198 setReadOnly(true);
156 setTextInteractionFlags(Qt::TextBrowserInteraction); 199 setTextInteractionFlags(Qt::TextBrowserInteraction);
157 setFrameShape(QFrame::NoFrame); 200 setFrameShape(QFrame::NoFrame);
159 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 202 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
160 setStyleSheet("background: transparent;"); 203 setStyleSheet("background: transparent;");
161 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 204 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
162 } 205 }
163 206
207 /* highly based upon... some stackoverflow answer for PyQt */
164 QSize Paragraph::minimumSizeHint() const { 208 QSize Paragraph::minimumSizeHint() const {
165 QTextDocument* doc = document(); 209 QTextDocument* doc = document();
166 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin())); 210 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin()));
167 return QSize(QPlainTextEdit::sizeHint().width(), (long)h); 211 return QSize(QPlainTextEdit::sizeHint().width(), (long)h);
168 } 212 }
169 213
170 QSize Paragraph::sizeHint() const { 214 QSize Paragraph::sizeHint() const {
171 return minimumSizeHint(); 215 return minimumSizeHint();
172 } 216 }
217
218 } // namespace UiUtils