diff --git a/kdevplatform/util/CMakeLists.txt b/kdevplatform/util/CMakeLists.txt --- a/kdevplatform/util/CMakeLists.txt +++ b/kdevplatform/util/CMakeLists.txt @@ -26,6 +26,7 @@ path.cpp texteditorhelpers.cpp stack.cpp + expandablelineedit.cpp ) set (KDevPlatformUtil_LIB_UI diff --git a/kdevplatform/util/expandablelineedit.h b/kdevplatform/util/expandablelineedit.h new file mode 100644 --- /dev/null +++ b/kdevplatform/util/expandablelineedit.h @@ -0,0 +1,50 @@ +/* + * Copyright 2018 Anton Anikin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library 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. + */ + +#ifndef EXPANDABLE_LINE_EDIT_H +#define EXPANDABLE_LINE_EDIT_H + +#include "utilexport.h" +#include + +/** This class implements a line edit widget which tries to expand its + * width to fit typed text. When no text typed or text is too short + * the widget's width is defined by the preferredWidth() value + * (default is 200 px). + */ + +class KDEVPLATFORMUTIL_EXPORT KExpandableLineEdit : public QLineEdit +{ + Q_OBJECT + +public: + explicit KExpandableLineEdit(QWidget* parent = nullptr); + + ~KExpandableLineEdit() override; + + int preferredWidth() const; + void setPreferredWidth(int width); + + QSize sizeHint() const override; + +protected: + int m_preferredWidth; +}; + +#endif diff --git a/kdevplatform/util/expandablelineedit.cpp b/kdevplatform/util/expandablelineedit.cpp new file mode 100644 --- /dev/null +++ b/kdevplatform/util/expandablelineedit.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2018 Anton Anikin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library 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 "expandablelineedit.h" + +KExpandableLineEdit::KExpandableLineEdit(QWidget* parent) + : QLineEdit(parent) + , m_preferredWidth(200) +{ + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + + connect(this, &KExpandableLineEdit::textChanged, this, &KExpandableLineEdit::updateGeometry); +} + +KExpandableLineEdit::~KExpandableLineEdit() +{ +} + +int KExpandableLineEdit::preferredWidth() const +{ + return m_preferredWidth; +} + +void KExpandableLineEdit::setPreferredWidth(int width) +{ + if (m_preferredWidth != width) { + m_preferredWidth = width; + updateGeometry(); + } +} + +QSize KExpandableLineEdit::sizeHint() const +{ + auto idealSize = QLineEdit::sizeHint(); + + int idealWidth = fontMetrics().width(text()); + if (isClearButtonEnabled()) { + idealWidth += 2 * idealSize.height(); + } + idealSize.setWidth(qMax(idealWidth, m_preferredWidth)); + + return idealSize; +} diff --git a/plugins/problemreporter/problemsview.h b/plugins/problemreporter/problemsview.h --- a/plugins/problemreporter/problemsview.h +++ b/plugins/problemreporter/problemsview.h @@ -27,14 +27,16 @@ class ProblemTreeView; class KActionMenu; +class KExpandableLineEdit; class QAction; class QActionGroup; class QLineEdit; class QTabWidget; namespace KDevelop { + struct ModelData; /** @@ -108,7 +110,7 @@ void setFilter(const QString& filterText); void setFilter(const QString& filterText, int tabIdx); - QLineEdit* m_filterEdit; + KExpandableLineEdit* m_filterEdit; int m_prevTabIdx; QVector m_models; }; diff --git a/plugins/problemreporter/problemsview.cpp b/plugins/problemreporter/problemsview.cpp --- a/plugins/problemreporter/problemsview.cpp +++ b/plugins/problemreporter/problemsview.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "problemtreeview.h" #include "problemmodel.h" @@ -214,14 +215,10 @@ setFilter(m_filterEdit->text()); }); - m_filterEdit = new QLineEdit(this); + m_filterEdit = new KExpandableLineEdit(this); m_filterEdit->setClearButtonEnabled(true); m_filterEdit->setPlaceholderText(i18n("Search...")); - QSizePolicy p(m_filterEdit->sizePolicy()); - p.setHorizontalPolicy(QSizePolicy::Fixed); - m_filterEdit->setSizePolicy(p); - connect(m_filterEdit, &QLineEdit::textChanged, this, [filterTimer](const QString&) { filterTimer->start(500); }); diff --git a/plugins/problemreporter/tests/CMakeLists.txt b/plugins/problemreporter/tests/CMakeLists.txt --- a/plugins/problemreporter/tests/CMakeLists.txt +++ b/plugins/problemreporter/tests/CMakeLists.txt @@ -5,5 +5,5 @@ ) ecm_add_test(${TEST_PROBLEMSVIEW_SRC} TEST_NAME test_problemsview - LINK_LIBRARIES Qt5::Test KDev::Tests KDev::Shell + LINK_LIBRARIES Qt5::Test KDev::Tests KDev::Shell KDev::Util ) diff --git a/plugins/standardoutputview/outputwidget.h b/plugins/standardoutputview/outputwidget.h --- a/plugins/standardoutputview/outputwidget.h +++ b/plugins/standardoutputview/outputwidget.h @@ -29,6 +29,7 @@ #include #include +class KExpandableLineEdit; class KToggleAction; class StandardOutputViewTest; class QAction; @@ -112,7 +113,7 @@ QAction* m_previousAction; KToggleAction* m_activateOnSelect; KToggleAction* m_focusOnSelect; - QLineEdit * m_filterInput; + KExpandableLineEdit* m_filterInput; QWidgetAction* m_filterAction; }; diff --git a/plugins/standardoutputview/outputwidget.cpp b/plugins/standardoutputview/outputwidget.cpp --- a/plugins/standardoutputview/outputwidget.cpp +++ b/plugins/standardoutputview/outputwidget.cpp @@ -44,6 +44,7 @@ #include #include +#include #include "outputmodel.h" #include "toolviewdata.h" @@ -155,9 +156,7 @@ separator->setSeparator(true); addAction(separator); - m_filterInput = new QLineEdit(); - m_filterInput->setMaximumWidth(150); - m_filterInput->setMinimumWidth(100); + m_filterInput = new KExpandableLineEdit(this); m_filterInput->setPlaceholderText(i18n("Search...")); m_filterInput->setClearButtonEnabled(true); m_filterInput->setToolTip(i18n("Enter a wild card string to filter the output view"));