From 77751c94f17e2c110ae9e88b1780e279d610b96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Sat, 15 Apr 2017 23:30:30 +0200 Subject: Qt: Add interface toolbar support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An extcap utility can provide configuration for controls to use in a GUI interface toolbar. This controls are bidirectional and can be used to control the extcap utility while capturing. This is useful in scenarios where configuration can be done based on findings in the capture process, setting temporary values or give other inputs without restarting current capture. Todo: - Add support for Windows Change-Id: Ie15fa67f92eb27d8b73df6bb36f66b9a7d81932d Reviewed-on: https://code.wireshark.org/review/19982 Petri-Dish: Stig Bjørlykke Tested-by: Petri Dish Buildbot Reviewed-by: Stig Bjørlykke --- ui/qt/interface_toolbar_lineedit.cpp | 149 +++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 ui/qt/interface_toolbar_lineedit.cpp (limited to 'ui/qt/interface_toolbar_lineedit.cpp') diff --git a/ui/qt/interface_toolbar_lineedit.cpp b/ui/qt/interface_toolbar_lineedit.cpp new file mode 100644 index 0000000000..181bcb3be5 --- /dev/null +++ b/ui/qt/interface_toolbar_lineedit.cpp @@ -0,0 +1,149 @@ +/* interface_toolbar_lineedit.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include "interface_toolbar_lineedit.h" +#include "stock_icon_tool_button.h" +#include "epan/prefs.h" +#include "color_utils.h" + +#include + +// To do: +// - Make a narrower apply button + +InterfaceToolbarLineEdit::InterfaceToolbarLineEdit(QWidget *parent, QString validation_regex, bool is_required) : + QLineEdit(parent), + validation_regex_(validation_regex), + is_required_(is_required), + text_edited_(false) +{ + apply_button_ = new StockIconToolButton(this, "x-filter-apply"); + apply_button_->setCursor(Qt::ArrowCursor); + apply_button_->setEnabled(false); + apply_button_->setToolTip(tr("Apply changes")); + apply_button_->setIconSize(QSize(24, 14)); + apply_button_->setStyleSheet( + "QToolButton {" + " border: none;" + " background: transparent;" // Disables platform style on Windows. + " padding: 0 0 0 0;" + "}" + ); + + updateStyleSheet(isValid()); + + connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(validateText())); + connect(this, SIGNAL(textEdited(const QString &)), this, SLOT(validateEditedText())); + connect(this, SIGNAL(returnPressed()), this, SLOT(applyEditedText())); + connect(apply_button_, SIGNAL(clicked()), this, SLOT(applyEditedText())); +} + +void InterfaceToolbarLineEdit::validateText() +{ + bool valid = isValid(); + + apply_button_->setEnabled(valid); + updateStyleSheet(valid); +} + +void InterfaceToolbarLineEdit::validateEditedText() +{ + text_edited_ = true; +} + +void InterfaceToolbarLineEdit::applyEditedText() +{ + if (text_edited_ && isValid()) + { + emit editedTextApplied(); + disableApplyButton(); + } +} + +void InterfaceToolbarLineEdit::disableApplyButton() +{ + apply_button_->setEnabled(false); + text_edited_ = false; +} + +bool InterfaceToolbarLineEdit::isValid() +{ + bool valid = true; + + if (is_required_ && text().length() == 0) + { + valid = false; + } + + if (!validation_regex_.isEmpty() && text().length() > 0) + { + QRegExp expr(validation_regex_); + if (!expr.isValid() || expr.indexIn(text(), 0) == -1) + { + valid = false; + } + } + + return valid; +} + +void InterfaceToolbarLineEdit::updateStyleSheet(bool is_valid) +{ + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + QSize apsz = apply_button_->sizeHint(); + + QString style_sheet = QString( + "InterfaceToolbarLineEdit {" + " padding-right: %1px;" + " background-color: %2;" + "}" + ) + .arg(apsz.width() + frameWidth) + .arg(is_valid ? QString("") : ColorUtils::fromColorT(prefs.gui_text_invalid).name()); + + setStyleSheet(style_sheet); +} + +void InterfaceToolbarLineEdit::resizeEvent(QResizeEvent *) +{ + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + QSize apsz = apply_button_->sizeHint(); + + apply_button_->move(contentsRect().right() - frameWidth - apsz.width() + 2, + contentsRect().top()); + apply_button_->setMinimumHeight(contentsRect().height()); + apply_button_->setMaximumHeight(contentsRect().height()); +} + +/* + * Editor modelines + * + * Local Variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ -- cgit v1.2.1