Mercurial > minori
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 |
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 | |
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 | 23 bool UiUtils::IsInDarkMode() { |
24 if (session.config.theme != OS) | |
25 return (session.config.theme == DARK); | |
26 #ifdef MACOSX | |
27 if (osx::DarkThemeAvailable()) { | |
28 if (osx::IsInDarkTheme()) { | |
29 return true; | |
30 } else { | |
31 return false; | |
32 } | |
33 } | |
34 #elif defined(WIN32) | |
35 if (win32::DarkThemeAvailable()) { | |
36 if (win32::IsInDarkTheme()) { | |
37 return true; | |
38 } else { | |
39 return false; | |
40 } | |
41 } | |
42 #endif | |
43 return (session.config.theme == DARK); | |
44 } | |
45 | |
7 | 46 void UiUtils::CreateTextHeader(QWidget* parent, QString title) { |
2 | 47 QLabel* static_text_title = new QLabel(title, parent); |
48 static_text_title->setTextFormat(Qt::PlainText); | |
7 | 49 |
50 QFont font = static_text_title->font(); | |
51 font.setWeight(QFont::Bold); | |
52 static_text_title->setFont(font); | |
53 | |
54 static_text_title->setFixedHeight(16); | |
55 parent->layout()->addWidget(static_text_title); | |
2 | 56 |
57 QFrame* static_text_line = new QFrame(parent); | |
58 static_text_line->setFrameShape(QFrame::HLine); | |
59 static_text_line->setFrameShadow(QFrame::Sunken); | |
7 | 60 static_text_line->setFixedHeight(2); |
61 parent->layout()->addWidget(static_text_line); | |
2 | 62 } |
63 | |
7 | 64 QPlainTextEdit* UiUtils::CreateTextParagraph(QWidget* parent, QString title, QString data) { |
65 QWidget* paragraph_master = new QWidget(parent); | |
66 paragraph_master->setLayout(new QVBoxLayout); | |
2 | 67 |
7 | 68 CreateTextHeader(paragraph_master, title); |
69 | |
70 Paragraph* paragraph = new Paragraph(data, paragraph_master); | |
2 | 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 | 73 paragraph->setWordWrapMode(QTextOption::NoWrap); |
7 | 74 paragraph->setContentsMargins(12, 0, 0, 0); |
75 | |
76 paragraph_master->layout()->addWidget(paragraph); | |
77 paragraph_master->layout()->setSpacing(0); | |
78 paragraph_master->layout()->setMargin(0); | |
79 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
80 | |
2 | 81 return paragraph; |
82 } | |
83 | |
7 | 84 QPlainTextEdit* UiUtils::CreateTextParagraphWithLabels(QWidget* parent, QString title, QString label, QString data) { |
85 QWidget* paragraph_master = new QWidget(parent); | |
86 paragraph_master->setLayout(new QVBoxLayout); | |
87 | |
88 CreateTextHeader(paragraph_master, title); | |
2 | 89 |
7 | 90 QWidget* paragraph_and_label = new QWidget(paragraph_master); |
91 paragraph_and_label->setLayout(new QHBoxLayout); | |
92 paragraph_and_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
93 | |
94 Paragraph* label_t = new Paragraph(label, paragraph_and_label); | |
2 | 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 | 97 label_t->setWordWrapMode(QTextOption::NoWrap); |
7 | 98 label_t->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
99 label_t->setFixedWidth(123); | |
2 | 100 |
7 | 101 Paragraph* paragraph = new Paragraph(data, paragraph_and_label); |
2 | 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 | 104 paragraph->setWordWrapMode(QTextOption::NoWrap); |
7 | 105 |
106 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(label_t, 0, Qt::AlignTop); | |
107 ((QBoxLayout*)paragraph_and_label->layout())->addWidget(paragraph, 0, Qt::AlignTop); | |
108 | |
109 paragraph_and_label->setContentsMargins(12, 0, 0, 0); | |
110 | |
111 paragraph_master->layout()->addWidget(paragraph_and_label); | |
112 paragraph_master->layout()->setSpacing(0); | |
113 paragraph_master->layout()->setMargin(0); | |
114 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
115 | |
2 | 116 return paragraph; |
117 } | |
118 | |
119 /* As far as I can tell, this is identical to the way Taiga implements it. | |
120 Kind of cool, I didn't even look into the source code for it :p */ | |
7 | 121 QPlainTextEdit* UiUtils::CreateSelectableTextParagraph(QWidget* parent, QString title, QString data) { |
122 QWidget* paragraph_master = new QWidget(parent); | |
123 paragraph_master->setLayout(new QVBoxLayout); | |
124 | |
125 CreateTextHeader(paragraph_master, title); | |
126 | |
127 QWidget* paragraph_widget = new QWidget(paragraph_master); | |
128 paragraph_widget->setLayout(new QHBoxLayout); | |
129 paragraph_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
2 | 130 |
7 | 131 Paragraph* text_edit = new Paragraph(data, paragraph_widget); |
132 | |
133 paragraph_widget->layout()->addWidget(text_edit); | |
134 | |
135 paragraph_widget->setContentsMargins(12, 0, 0, 0); | |
136 | |
137 paragraph_master->layout()->addWidget(paragraph_widget); | |
138 paragraph_master->layout()->setSpacing(0); | |
139 paragraph_master->layout()->setMargin(0); | |
140 | |
141 paragraph_master->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
142 | |
2 | 143 return text_edit; |
144 } | |
7 | 145 |
146 void UiUtils::SetPlainTextEditData(QPlainTextEdit* text_edit, QString data) { | |
147 QTextDocument* document = new QTextDocument(text_edit); | |
148 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
149 document->setPlainText(data); | |
150 text_edit->setDocument(document); | |
151 } | |
152 | |
153 Paragraph::Paragraph(QString text, QWidget* parent) | |
154 : QPlainTextEdit(text, parent) { | |
155 setReadOnly(true); | |
156 setTextInteractionFlags(Qt::TextBrowserInteraction); | |
157 setFrameShape(QFrame::NoFrame); | |
158 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
159 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
160 setStyleSheet("background: transparent;"); | |
161 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | |
162 } | |
163 | |
164 QSize Paragraph::minimumSizeHint() const { | |
165 QTextDocument* doc = document(); | |
166 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + (2 * doc->documentMargin())); | |
167 return QSize(QPlainTextEdit::sizeHint().width(), (long)h); | |
168 } | |
169 | |
170 QSize Paragraph::sizeHint() const { | |
171 return minimumSizeHint(); | |
172 } |