diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CommandParser.cpp | 5 | ||||
| -rw-r--r-- | src/completion/CommandsAdapter.cpp | 22 | ||||
| -rw-r--r-- | src/completion/Completer.cpp | 4 | ||||
| -rw-r--r-- | src/completion/UrlAdapter.cpp | 20 | ||||
| -rw-r--r-- | src/completion/UrlModel.cpp | 48 | ||||
| -rw-r--r-- | src/widgets/InputLine.cpp (renamed from src/widgets/CommandInput.cpp) | 51 | ||||
| -rw-r--r-- | src/widgets/MainWindow.cpp | 63 |
7 files changed, 161 insertions, 52 deletions
diff --git a/src/CommandParser.cpp b/src/CommandParser.cpp index 7a8eb00..8f83575 100644 --- a/src/CommandParser.cpp +++ b/src/CommandParser.cpp @@ -6,6 +6,7 @@ CommandParser::CommandParser() {} Cmd CommandParser::parse(QString input) { + // TODO: simplify this to only parse the command auto parts = input.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts); if (parts.isEmpty()) @@ -13,9 +14,7 @@ Cmd CommandParser::parse(QString input) { auto cmdStr = parts.first(); auto cmd = toCommandType(cmdStr); - auto rawArgs = input.slice(cmdStr.length()); - - parts.removeFirst(); + auto rawArgs = input.slice(cmdStr.length()).trimmed(); return {.command = cmd, .argsString = rawArgs, .rawInput = input}; } diff --git a/src/completion/CommandsAdapter.cpp b/src/completion/CommandsAdapter.cpp new file mode 100644 index 0000000..df71661 --- /dev/null +++ b/src/completion/CommandsAdapter.cpp @@ -0,0 +1,22 @@ +#include <QWidget> +#include <QtCore> + +#include "completion/CommandsAdapter.hpp" +#include "completion/CommandsModel.hpp" +#include "completion/Completer.hpp" + +const QString cmdPrompt = "[exec]"; + +CommandsAdapter::CommandsAdapter() : Adapter() { + completerInstance = new Completer(this); + completerInstance->setModel(new CommandsModel(this)); +} + +Completer *CommandsAdapter::completer() { return completerInstance; } + +QString CommandsAdapter::prompt() { return cmdPrompt; } + +CommandsAdapter::~CommandsAdapter() { + delete completerInstance->model(); + delete completerInstance; +} diff --git a/src/completion/Completer.cpp b/src/completion/Completer.cpp index 96a363e..a38d3f2 100644 --- a/src/completion/Completer.cpp +++ b/src/completion/Completer.cpp @@ -27,9 +27,7 @@ Completer::Completer(QObject *parent) : QCompleter(parent) { treeView->setRootIsDecorated(false); treeView->setUniformRowHeights(true); treeView->header()->hide(); - treeView->setColumnWidth(1, 160); - - complete(); + treeView->setColumnWidth(1, 300); } // bool Completer::eventFilter(QObject *watched, QEvent *event) { diff --git a/src/completion/UrlAdapter.cpp b/src/completion/UrlAdapter.cpp new file mode 100644 index 0000000..2f1bb08 --- /dev/null +++ b/src/completion/UrlAdapter.cpp @@ -0,0 +1,20 @@ +#include <QWidget> +#include <QtCore> + +#include "completion/Completer.hpp" +#include "completion/UrlAdapter.hpp" +#include "completion/UrlModel.hpp" + +UrlAdapter::UrlAdapter() : Adapter() { + completerInstance = new Completer(this); + completerInstance->setModel(new UrlModel(this)); +} + +Completer *UrlAdapter::completer() { return completerInstance; } + +QString UrlAdapter::prompt() { return "[url]"; } + +UrlAdapter::~UrlAdapter() { + delete completerInstance->model(); + delete completerInstance; +} diff --git a/src/completion/UrlModel.cpp b/src/completion/UrlModel.cpp new file mode 100644 index 0000000..7671fc3 --- /dev/null +++ b/src/completion/UrlModel.cpp @@ -0,0 +1,48 @@ +#include <QAbstractListModel> +#include <QWidget> +#include <QtCore> + +#include "completion/UrlModel.hpp" + +UrlModel::UrlModel(QObject *parent) : QAbstractListModel(parent) { + items = { + {.url = "https://google.com", .description = "Google"}, + {.url = "https://duckduckgo.com", .description = "DuckDuckGo"}, + {.url = "https://ediblemonad.dev", .description = "EdibleMonad website"}, + {.url = "https://lite.duckduckgo.com", .description = "DDG lite"}, + {.url = "https://github.com/trending", .description = "Github trending"}, + }; +} + +QVariant UrlModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || index.row() >= items.size()) + return QVariant(); + + const Url &item = items.at(index.row()); + + if (role == Qt::DisplayRole) + return item.url; + else if (role == Qt::ToolTipRole) + return item.description; + else if (role == Qt::UserRole) + return item.description; + + return QVariant(); +} + +QHash<int, QByteArray> UrlModel::roleNames() const { + QHash<int, QByteArray> roles; + roles[Qt::DisplayRole] = "url"; + roles[Qt::UserRole] = "description"; + return roles; +} + +int UrlModel::rowCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return items.size(); +} + +int UrlModel::columnCount(const QModelIndex &parent) const { + Q_UNUSED(parent); + return 2; // name + description +} diff --git a/src/widgets/CommandInput.cpp b/src/widgets/InputLine.cpp index 3ea0f38..d577e1d 100644 --- a/src/widgets/CommandInput.cpp +++ b/src/widgets/InputLine.cpp @@ -1,20 +1,17 @@ +#include <QBoxLayout> #include <QCompleter> #include <QHBoxLayout> #include <QKeyEvent> #include <QLabel> #include <QLineEdit> #include <QStringListModel> -#include <QVBoxLayout> #include <QWidget> #include <QWidgetAction> #include <QWindow> -#include <QtCore/qnamespace.h> -#include <QtCore/qstringlistmodel.h> -#include <QtWidgets/qboxlayout.h> +#include <QtCore> -#include "completion/CommandsModel.hpp" -#include "completion/Completer.hpp" -#include "widgets/CommandInput.hpp" +#include "completion/CommandsAdapter.hpp" +#include "widgets/InputLine.hpp" const char *rootStyles = R"( background-color: #000; @@ -22,7 +19,13 @@ const char *rootStyles = R"( border-radius: 0; )"; -CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) +const char *promptStyles = R"( + background-color: #aaa; + color: #555; + padding: 0 2px; +)"; + +InputLine::InputLine(QString defaultInput, QWidget *parentNode) : QWidget(parentNode) { setContentsMargins(0, 0, 0, 0); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); @@ -31,10 +34,6 @@ CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) auto layout = new QVBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); - auto listModel = new CommandsModel(); - auto completer = new Completer(this); - completer->setModel(listModel); - auto cmdLineBox = new QWidget(); layout->addWidget(cmdLineBox); auto cmdLineLayout = new QHBoxLayout(); @@ -42,22 +41,30 @@ CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) cmdLineBox->layout()->setContentsMargins(0, 0, 0, 0); cmdLineLayout->setSpacing(0); - auto promptPrefix = new QLabel(tr(":")); + promptPrefix = new QLabel(); + promptPrefix->setStyleSheet(promptStyles); promptPrefix->setContentsMargins(0, 0, 0, 0); input = new QLineEdit(defaultInput); input->setFocusPolicy(Qt::StrongFocus); - input->setCompleter(completer); - // input->installEventFilter(completer); cmdLineLayout->addWidget(promptPrefix); cmdLineLayout->addWidget(input); - setFixedHeight(input->sizeHint().height()); + + setAdapter(new CommandsAdapter()); +} + +void InputLine::setAdapter(Adapter *adapter) { + if (this->adapter) + delete this->adapter; + this->adapter = adapter; + promptPrefix->setText(adapter->prompt()); + input->setCompleter(adapter->completer()); } -void CommandInput::emitSubmit() { emit submitted(getInputCommand()); } +void InputLine::emitSubmit() { emit submitted(getInputCommand()); } -void CommandInput::keyPressEvent(QKeyEvent *event) { +void InputLine::keyPressEvent(QKeyEvent *event) { auto combo = event->keyCombination(); auto ctrlL = combo.key() == Qt::Key_L && combo.keyboardModifiers().testFlag(Qt::ControlModifier); @@ -70,15 +77,15 @@ void CommandInput::keyPressEvent(QKeyEvent *event) { emitSubmit(); } -void CommandInput::setInputText(QString text) { input->setText(text); } +void InputLine::setInputText(QString text) { input->setText(text); } -bool CommandInput::isInputFocussed() { return input->hasFocus(); } +bool InputLine::isInputFocussed() { return input->hasFocus(); } -void CommandInput::setInputFocus(bool focussed) { +void InputLine::setInputFocus(bool focussed) { if (focussed) input->setFocus(Qt::PopupFocusReason); else input->clearFocus(); } -QString CommandInput::getInputCommand() { return input->text(); } +QString InputLine::getInputCommand() { return input->text(); } diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp index 3191719..03a4e6e 100644 --- a/src/widgets/MainWindow.cpp +++ b/src/widgets/MainWindow.cpp @@ -2,10 +2,13 @@ #include <QStackedLayout> #include <QVBoxLayout> #include <QWebEngineView> +#include <QtCore/qnamespace.h> #include "CommandParser.hpp" +#include "completion/CommandsAdapter.hpp" +#include "completion/UrlAdapter.hpp" #include "widgets/BrowserManager.hpp" -#include "widgets/CommandInput.hpp" +#include "widgets/InputLine.hpp" #include "widgets/MainWindow.hpp" MainWindow::MainWindow() { @@ -24,14 +27,12 @@ MainWindow::MainWindow() { layout->addWidget(browserManager); // Command input - commandInput = new CommandInput; - hideURLInput(); - commandInput->move(0, 0); - connect(commandInput, &CommandInput::submitted, this, - &MainWindow::evaluateCommand); - connect(commandInput, &CommandInput::cancelled, this, - &MainWindow::hideURLInput); - layout->addWidget(commandInput); + inputLine = new InputLine; + inputLine->setHidden(true); + inputLine->move(0, 0); + connect(inputLine, &InputLine::submitted, this, &MainWindow::evaluateCommand); + connect(inputLine, &InputLine::cancelled, this, &MainWindow::hideInputLine); + layout->addWidget(inputLine); // Lua runtime luaRuntime = LuaRuntime::instance(); @@ -41,20 +42,31 @@ MainWindow::MainWindow() { }); } -void MainWindow::hideURLInput() { - commandInput->setInputFocus(false); - commandInput->setHidden(true); +void MainWindow::hideInputLine() { + inputLine->setInputFocus(false); + inputLine->setHidden(true); } -void MainWindow::showURLInput() { - commandInput->setInputText("open " + browserManager->currentUrl().toString()); - commandInput->setHidden(false); - layout->setCurrentWidget(commandInput); - commandInput->setInputFocus(true); +void MainWindow::showInputLine() { + inputLine->setHidden(false); + layout->setCurrentWidget(inputLine); + inputLine->setInputFocus(true); +} + +void MainWindow::showURLInput(QString url) { + showInputLine(); + inputLine->setAdapter(new UrlAdapter); + inputLine->setInputText(url); +} + +void MainWindow::showCommandInput(QString cmd) { + showInputLine(); + inputLine->setAdapter(new CommandsAdapter); + inputLine->setInputText(cmd); } void MainWindow::evaluateCommand(QString command) { - hideURLInput(); + hideInputLine(); CommandParser parser; auto cmd = parser.parse(command); @@ -64,7 +76,11 @@ void MainWindow::evaluateCommand(QString command) { luaRuntime->evaluate(cmd.argsString); break; case CommandType::Open: - browserManager->openUrl(cmd.argsString, OpenType::OpenUrl); + if (cmd.argsString.trimmed().isEmpty()) { + showURLInput(); + } else { + browserManager->openUrl(cmd.argsString, OpenType::OpenUrl); + } break; case CommandType::TabOpen: browserManager->openUrl(cmd.argsString, OpenType::OpenUrlInTab); @@ -84,7 +100,10 @@ void MainWindow::keyPressEvent(QKeyEvent *event) { auto combo = event->keyCombination(); if (combo.key() == Qt::Key_L && combo.keyboardModifiers().testFlag(Qt::ControlModifier)) { - toggleURLInput(); + showURLInput(browserManager->currentUrl().toString()); + } else if (combo.key() == Qt::Key_Semicolon && + combo.keyboardModifiers().testFlag(Qt::ControlModifier)) { + showCommandInput(); } else if (combo.key() == Qt::Key_T && combo.keyboardModifiers().testFlag(Qt::ControlModifier)) { browserManager->createNewWebView(QUrl("https://lite.duckduckgo.com"), true); @@ -99,7 +118,3 @@ void MainWindow::keyPressEvent(QKeyEvent *event) { browserManager->closeCurrentWebView(); } } - -void MainWindow::toggleURLInput() { - commandInput->isHidden() ? showURLInput() : hideURLInput(); -} |
