diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-06 16:46:36 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-06 16:46:36 +0200 |
| commit | 7772993ed5d65f10588f3d70961c429991fd7849 (patch) | |
| tree | eb90d892fc11731cfa1ca322f783204a727e23f2 /browsertab.cpp | |
| parent | bcda97a2e17f6e1366cfe5b03bd0b407d4484255 (diff) | |
| download | kristall-7772993ed5d65f10588f3d70961c429991fd7849.tar.gz | |
Starts to implement settings dialog, allows styling the fonts, fixes stop/reload button, refactors text document rendering into own file, fixes stuff in README
Diffstat (limited to 'browsertab.cpp')
| -rw-r--r-- | browsertab.cpp | 215 |
1 files changed, 18 insertions, 197 deletions
diff --git a/browsertab.cpp b/browsertab.cpp index 153e19f..19a1993 100644 --- a/browsertab.cpp +++ b/browsertab.cpp @@ -1,6 +1,8 @@ #include "browsertab.hpp" #include "ui_browsertab.h" #include "mainwindow.hpp" +#include "geminirenderer.hpp" +#include "settingsdialog.hpp" #include <QTabWidget> #include <QMenu> @@ -9,12 +11,11 @@ #include <QDockWidget> #include <QImage> #include <QPixmap> -#include <QTextList> -#include <QTextBlock> #include <QGraphicsPixmapItem> #include <QGraphicsTextItem> + BrowserTab::BrowserTab(MainWindow * mainWindow) : QWidget(nullptr), ui(new Ui::BrowserTab), @@ -94,6 +95,12 @@ void BrowserTab::on_menu_button_clicked() } } + connect(menu.addAction("Settings..."), &QAction::triggered, [this]() { + SettingsDialog dialog; + + dialog.exec(); + }); + connect(menu.addAction("Quit"), &QAction::triggered, &QApplication::quit); menu.exec(QCursor::pos()); @@ -127,7 +134,7 @@ void BrowserTab::on_gemini_complete(const QByteArray &data, const QString &mime) if(mime.startsWith("text/gemini")) { - document = translateGemini(data, this->current_location, this->outline); + document = GeminiRenderer{}.render(data, this->current_location, this->outline); } else if(mime.startsWith("text/html")) { document = std::make_unique<QTextDocument>(); @@ -368,6 +375,12 @@ void BrowserTab::on_text_browser_highlighted(const QUrl &url) this->mainWindow->setUrlPreview(real_url); } +void BrowserTab::on_stop_button_clicked() +{ + gemini_client.cancelRequest(); +} + + void BrowserTab::on_back_button_clicked() { @@ -383,201 +396,9 @@ void BrowserTab::updateUI() this->ui->back_button->setEnabled(this->history.canGoBack()); this->ui->forward_button->setEnabled(this->history.canGoForward()); - this->ui->refresh_button->setVisible(not this->gemini_client.isInProgress()); - this->ui->stop_button->setVisible(this->gemini_client.isInProgress()); + this->ui->refresh_button->setVisible(this->successfully_loaded); + this->ui->stop_button->setVisible(not this->successfully_loaded); this->ui->fav_button->setEnabled(this->successfully_loaded); this->ui->fav_button->setChecked(this->mainWindow->favourites.contains(this->current_location)); } - -QByteArray trim_whitespace(QByteArray items) -{ - int start = 0; - while(start < items.size() and isspace(items.at(start))) { - start += 1; - } - int end = items.size() - 1; - while(end > 0 and isspace(items.at(end))) { - end -= 1; - } - return items.mid(start, end - start + 1); -} - -std::unique_ptr<QTextDocument> BrowserTab::translateGemini(const QByteArray &input, QUrl const & root_url, DocumentOutlineModel &outline) -{ - QFont preformatted_font; - preformatted_font.setFamily("monospace"); - preformatted_font.setPointSizeF(10.0); - - QFont standard_font; - standard_font.setFamily("sans"); - standard_font.setPointSizeF(10.0); - - QFont h1_font; - h1_font.setFamily("sans"); - h1_font.setBold(true); - h1_font.setPointSizeF(20.0); - - QFont h2_font; - h2_font.setFamily("sans"); - h2_font.setBold(true); - h2_font.setPointSizeF(15.0); - - QFont h3_font; - h3_font.setFamily("sans"); - h3_font.setBold(true); - h3_font.setPointSizeF(12.0); - - QTextCharFormat preformatted; - preformatted.setFont(preformatted_font); - - QTextCharFormat standard; - standard.setFont(standard_font); - - QTextCharFormat standard_link; - standard_link.setFont(standard_font); - standard_link.setForeground(QBrush(QColor(0,128,255))); - - QTextCharFormat external_link; - external_link.setFont(standard_font); - external_link.setForeground(QBrush(QColor(0,0,255))); - - QTextCharFormat cross_protocol_link; - cross_protocol_link.setFont(standard_font); - cross_protocol_link.setForeground(QBrush(QColor(128,0,255))); - - QTextCharFormat standard_h1; - standard_h1.setFont(h1_font); - standard_h1.setForeground(QBrush(QColor(255,0,0))); - - QTextCharFormat standard_h2; - standard_h2.setFont(h2_font); - standard_h2.setForeground(QBrush(QColor(0,128,0))); - - QTextCharFormat standard_h3; - standard_h3.setFont(h3_font); - standard_h3.setForeground(QBrush(QColor(32,255,0))); - - std::unique_ptr<QTextDocument> result = std::make_unique<QTextDocument>(); - result->setDocumentMargin(55.0); - - QTextCursor cursor { result.get() }; - - QTextBlockFormat non_list_format = cursor.blockFormat(); - - bool verbatim = false; - QTextList * current_list = nullptr; - - outline.beginBuild(); - - QList<QByteArray> lines = input.split('\n'); - for(auto const & line : lines) - { - if(verbatim) { - if(line.startsWith("```")) { - verbatim = false; - } - else { - cursor.setCharFormat(preformatted); - cursor.insertText(line + "\n"); - } - } else { - if(line.startsWith("*")) { - if(current_list == nullptr) { - cursor.deletePreviousChar(); - current_list = cursor.insertList(QTextListFormat::ListDisc); - } else { - cursor.insertBlock(); - } - - QString item = trim_whitespace(line.mid(1)); - - cursor.insertText(item, standard); - continue; - } else { - if(current_list != nullptr) { - cursor.insertBlock(); - cursor.setBlockFormat(non_list_format); - } - current_list = nullptr; - } - - if(line.startsWith("###")) { - auto heading = trim_whitespace(line.mid(3)); - - cursor.insertText(heading, standard_h3); - cursor.insertBlock(); - outline.appendH3(heading); - } - else if(line.startsWith("##")) { - auto heading = trim_whitespace(line.mid(2)); - - cursor.insertText(heading, standard_h2); - cursor.insertBlock(); - outline.appendH2(heading); - } - else if(line.startsWith("#")) { - auto heading = trim_whitespace(line.mid(1)); - - cursor.insertText(heading, standard_h1); - outline.appendH1(heading); - } - else if(line.startsWith("=>")) { - auto const part = line.mid(2).trimmed(); - - QByteArray link, title; - - int index = -1; - for(int i = 0; i < part.size(); i++) { - if(isspace(part[i])) { - index = i; - break; - } - } - - if(index > 0) { - link = trim_whitespace(part.mid(0, index)); - title = trim_whitespace(part.mid(index + 1)); - } else { - link = trim_whitespace(part); - title = trim_whitespace(part); - } - - auto local_url = QUrl(link); - - auto absolute_url = root_url.resolved(QUrl(link)); - - // qDebug() << link << title; - - auto fmt = standard_link; - if(not local_url.isRelative()) { - fmt = external_link; - } - - QString suffix = ""; - if(absolute_url.scheme() != root_url.scheme()) { - suffix = " [" + absolute_url.scheme().toUpper() + "]"; - fmt = cross_protocol_link; - } - - fmt.setAnchor(true); - fmt.setAnchorHref(absolute_url.toString()); - - if(local_url.isRelative()) { - cursor.insertText("→ " + title + suffix + "\n", fmt); - } else { - cursor.insertText("⇒ " + title + suffix + "\n", fmt); - } - } - else if(line.startsWith("```")) { - verbatim = true; - } - else { - cursor.insertText(line + "\n", standard); - } - } - } - - outline.endBuild(); - return result; -} |
