File indexing completed on 2024-03-24 03:48:12
0001 /* 0002 SPDX-FileCopyrightText: 2003-2009 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "console.h" 0008 #include "editor.h" // only for the error highlight color value 0009 0010 #include <QApplication> 0011 #include <QComboBox> 0012 #include <QFontDatabase> 0013 #include <QLabel> 0014 #include <QLineEdit> 0015 #include <QHBoxLayout> 0016 0017 #include <KLocalizedString> 0018 0019 0020 Console::Console(QWidget* parent) 0021 : QWidgetAction(parent) 0022 { 0023 baseWidget = new QWidget(parent); 0024 QHBoxLayout* baseLayout = new QHBoxLayout(); 0025 baseLayout->setContentsMargins(0, 0, 0, 0); 0026 baseWidget->setLayout(baseLayout); 0027 0028 comboBox = new QComboBox(baseWidget); 0029 comboBox->setEditable(true); 0030 comboBox->setMinimumWidth(200); 0031 comboBox->setDuplicatesEnabled(true); 0032 comboBox->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); 0033 comboBox->setToolTip(i18n("Write a command here and press enter...")); 0034 comboBox->setWhatsThis(i18n("Console: quickly run single commands -- write a command here and press enter.")); 0035 0036 QLabel* consoleLabel = new QLabel(i18n("Console:"), baseWidget); 0037 consoleLabel->setBuddy(comboBox); 0038 consoleLabel->setWhatsThis(comboBox->whatsThis()); 0039 0040 baseLayout->addWidget(consoleLabel); 0041 baseLayout->addWidget(comboBox); 0042 setDefaultWidget(baseWidget); 0043 0044 connect(comboBox->lineEdit(), &QLineEdit::returnPressed, this, &Console::run); 0045 connect(comboBox, &QComboBox::editTextChanged, this, &Console::clearMarkings); 0046 } 0047 0048 void Console::disable() 0049 { 0050 comboBox->setEnabled(false); 0051 } 0052 0053 void Console::enable() 0054 { 0055 comboBox->setEnabled(true); 0056 } 0057 0058 void Console::clearMarkings() 0059 { 0060 comboBox->setToolTip(i18n("Write a command here and press enter...")); 0061 comboBox->setStyleSheet(QLatin1String("")); 0062 comboBox->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); 0063 } 0064 0065 void Console::run() 0066 { 0067 QString errorMessage = Q_EMIT execute(comboBox->currentText()); 0068 if (errorMessage.isNull()) { 0069 comboBox->clearEditText(); 0070 return; 0071 } 0072 showError(errorMessage); 0073 } 0074 0075 void Console::showError(const QString& msg) 0076 { 0077 comboBox->setStyleSheet(QStringLiteral("QComboBox:editable{background:") + ERROR_HIGHLIGHT_COLOR.name() + QStringLiteral(";}")); 0078 comboBox->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); 0079 QString toolTipText(i18n("<p style='white-space:pre'><b>ERROR:</b> %1</p>", msg)); 0080 comboBox->setToolTip(toolTipText); 0081 } 0082 0083 void Console::executeActionTriggered() 0084 { 0085 QLineEdit* lineEdit = comboBox->lineEdit(); 0086 if (!lineEdit) 0087 return; 0088 QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, QStringLiteral("\n")); 0089 QApplication::sendEvent(lineEdit, &event); 0090 } 0091 0092 #include "moc_console.cpp"