Mercurial > minori
annotate src/gui/ui_utils.cpp @ 11:fc1bf97c528b
*: use C++11 standard
I've been meaning to do this for a while, but I didn't want to reimplement
the filesystem code. Now we are on C++11 and most compilers from the past 5 centuries should support this now
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 17 Sep 2023 06:14:30 -0400 |
| parents | 5c0397762b53 |
| children | cde8f67a7c7d |
| rev | line source |
|---|---|
| 9 | 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> | |
| 7 | 8 #include <QLabel> |
| 9 | 9 #include <QPixmap> |
| 10 #include <QTextBlock> | |
| 7 | 11 #include <QVBoxLayout> |
| 2 | 12 #ifdef MACOSX |
| 13 #include "sys/osx/dark_theme.h" | |
| 14 #else | |
| 15 #include "sys/win32/dark_theme.h" | |
| 16 #endif | |
| 17 | |
| 8 | 18 namespace UiUtils { |
| 19 | |
| 20 bool IsInDarkMode() { | |
| 9 | 21 if (session.config.theme != Themes::OS) |
| 22 return (session.config.theme == Themes::DARK); | |
| 2 | 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 | |
| 9 | 40 return (session.config.theme == Themes::DARK); |
| 2 | 41 } |
| 42 | |
| 9 | 43 Header::Header(QString title, QWidget* parent) : QWidget(parent) { |
| 8 | 44 setLayout(new QVBoxLayout); |
| 45 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | |
| 46 | |
| 9 | 47 static_text_title = new QLabel(title, this); |
| 2 | 48 static_text_title->setTextFormat(Qt::PlainText); |
| 7 | 49 QFont font = static_text_title->font(); |
| 50 font.setWeight(QFont::Bold); | |
| 51 static_text_title->setFont(font); | |
| 8 | 52 static_text_title->setFixedHeight(16); |
| 7 | 53 |
| 8 | 54 static_text_line = new QFrame(this); |
| 2 | 55 static_text_line->setFrameShape(QFrame::HLine); |
| 56 static_text_line->setFrameShadow(QFrame::Sunken); | |
| 7 | 57 static_text_line->setFixedHeight(2); |
| 8 | 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); | |
| 2 | 67 } |
| 68 | |
| 9 | 69 TextParagraph::TextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { |
| 8 | 70 setLayout(new QVBoxLayout); |
| 7 | 71 |
| 8 | 72 header = new Header(title, this); |
| 2 | 73 |
| 8 | 74 QWidget* content = new QWidget(this); |
| 75 content->setLayout(new QHBoxLayout); | |
| 7 | 76 |
| 8 | 77 paragraph = new Paragraph(data, this); |
| 2 | 78 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); |
|
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
79 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); |
| 2 | 80 paragraph->setWordWrapMode(QTextOption::NoWrap); |
| 7 | 81 |
| 8 | 82 content->layout()->addWidget(paragraph); |
| 83 content->layout()->setSpacing(0); | |
| 84 content->layout()->setMargin(0); | |
| 85 content->setContentsMargins(12, 0, 0, 0); | |
| 7 | 86 |
| 8 | 87 layout()->addWidget(header); |
| 88 layout()->addWidget(paragraph); | |
| 89 layout()->setSpacing(0); | |
| 90 layout()->setMargin(0); | |
| 91 } | |
| 7 | 92 |
| 8 | 93 Header* TextParagraph::GetHeader() { |
| 94 return header; | |
| 95 } | |
| 96 | |
| 97 Paragraph* TextParagraph::GetParagraph() { | |
| 2 | 98 return paragraph; |
| 99 } | |
| 100 | |
| 8 | 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); | |
| 7 | 106 |
| 8 | 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); | |
| 7 | 111 |
| 8 | 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); | |
| 2 | 118 |
| 8 | 119 paragraph = new Paragraph(data, this); |
| 120 paragraph->setTextInteractionFlags(Qt::NoTextInteraction); | |
| 121 paragraph->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents); | |
| 122 paragraph->setWordWrapMode(QTextOption::NoWrap); | |
| 7 | 123 |
| 8 | 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); | |
| 7 | 130 |
| 8 | 131 content->setContentsMargins(12, 0, 0, 0); |
| 7 | 132 |
| 8 | 133 layout()->addWidget(header); |
| 134 layout()->addWidget(content); | |
| 135 layout()->setSpacing(0); | |
| 136 layout()->setMargin(0); | |
| 2 | 137 } |
| 7 | 138 |
| 8 | 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 | |
| 9 | 151 SelectableTextParagraph::SelectableTextParagraph(QString title, QString data, QWidget* parent) : QWidget(parent) { |
| 8 | 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) { | |
| 7 | 181 QTextDocument* document = new QTextDocument(text_edit); |
| 182 document->setDocumentLayout(new QPlainTextDocumentLayout(document)); | |
| 183 document->setPlainText(data); | |
| 184 text_edit->setDocument(document); | |
| 185 } | |
| 186 | |
| 8 | 187 /* inherits QPlainTextEdit and gives a much more reasonable minimum size */ |
| 9 | 188 Paragraph::Paragraph(QString text, QWidget* parent) : QPlainTextEdit(text, parent) { |
| 7 | 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 | |
| 8 | 198 /* highly based upon... some stackoverflow answer for PyQt */ |
| 7 | 199 QSize Paragraph::minimumSizeHint() const { |
| 200 QTextDocument* doc = document(); | |
| 9 | 201 long h = (long)(blockBoundingGeometry(doc->findBlockByNumber(doc->blockCount() - 1)).bottom() + |
| 202 (2 * doc->documentMargin())); | |
| 7 | 203 return QSize(QPlainTextEdit::sizeHint().width(), (long)h); |
| 204 } | |
| 205 | |
| 206 QSize Paragraph::sizeHint() const { | |
| 207 return minimumSizeHint(); | |
| 208 } | |
| 8 | 209 |
| 210 } // namespace UiUtils | |
| 9 | 211 |
| 212 #include "gui/moc_ui_utils.cpp" |
