diff options
| author | AL <26797547+Al12rs@users.noreply.github.com> | 2020-02-01 21:28:14 +0100 |
|---|---|---|
| committer | AL <26797547+Al12rs@users.noreply.github.com> | 2020-02-01 21:28:14 +0100 |
| commit | 326c9130c43ec73a9d12ea65d92b60821d4f8e91 (patch) | |
| tree | fd21871a39ca48e98a92e88c4ec6a35631ee191a | |
| parent | 9c8412a051013f0ba8b70d4cb56d3d0428d827f0 (diff) | |
Moved filterWidget and eventFilter to ui base
| -rw-r--r-- | src/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/eventfilter.cpp | 33 | ||||
| -rw-r--r-- | src/eventfilter.h | 44 | ||||
| -rw-r--r-- | src/filterwidget.cpp | 221 | ||||
| -rw-r--r-- | src/filterwidget.h | 70 |
5 files changed, 0 insertions, 374 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d0215930..5d81f9da 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -122,13 +122,11 @@ SET(organizer_SRCS organizercore.cpp instancemanager.cpp usvfsconnector.cpp - eventfilter.cpp moshortcut.cpp listdialog.cpp lcdnumber.cpp forcedloaddialog.cpp forcedloaddialogwidget.cpp - filterwidget.cpp statusbar.cpp apiuseraccount.cpp filerenamer.cpp @@ -248,13 +246,11 @@ SET(organizer_HDRS iuserinterface.h instancemanager.h usvfsconnector.h - eventfilter.h moshortcut.h listdialog.h lcdnumber.h forcedloaddialog.h forcedloaddialogwidget.h - filterwidget.h statusbar.h apiuseraccount.h filerenamer.h @@ -471,7 +467,6 @@ set(utilities bbcode csvbuilder shared/error_report - eventfilter shared/leaktrace persistentcookiejar serverinfo @@ -487,7 +482,6 @@ set(widgets colortable genericicondelegate filerenamer - filterwidget icondelegate lcdnumber loglist diff --git a/src/eventfilter.cpp b/src/eventfilter.cpp deleted file mode 100644 index 69327f79..00000000 --- a/src/eventfilter.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright (C) 2016 Sebastian Herbord. All rights reserved. - -This file is part of Mod Organizer. - -Mod Organizer 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 3 of the License, or -(at your option) any later version. - -Mod Organizer 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 Mod Organizer. If not, see <http://www.gnu.org/licenses/>. -*/ - - -#include "eventfilter.h" - -EventFilter::EventFilter(QObject *parent, - const EventFilter::HandlerFunc &handler) - : QObject(parent) - , m_Handler(handler) -{ -} - -bool EventFilter::eventFilter(QObject *obj, QEvent *event) -{ - return m_Handler(obj, event); -} diff --git a/src/eventfilter.h b/src/eventfilter.h deleted file mode 100644 index 2400a853..00000000 --- a/src/eventfilter.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright (C) 2016 Sebastian Herbord. All rights reserved. - -This file is part of Mod Organizer. - -Mod Organizer 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 3 of the License, or -(at your option) any later version. - -Mod Organizer 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 Mod Organizer. If not, see <http://www.gnu.org/licenses/>. -*/ - -#pragma once - - -#include <QObject> -#include <functional> - - -class EventFilter : public QObject { - - Q_OBJECT - - typedef std::function<bool (QObject*, QEvent*)> HandlerFunc; - -public: - - EventFilter(QObject *parent, const HandlerFunc &handler); - - virtual bool eventFilter(QObject *obj , QEvent *event) override; - -private: - - HandlerFunc m_Handler; - -}; - diff --git a/src/filterwidget.cpp b/src/filterwidget.cpp deleted file mode 100644 index 0638add3..00000000 --- a/src/filterwidget.cpp +++ /dev/null @@ -1,221 +0,0 @@ -#include "filterwidget.h" -#include "eventfilter.h" -#include <log.h> - -using namespace MOBase; - -FilterWidgetProxyModel::FilterWidgetProxyModel(FilterWidget& fw, QWidget* parent) - : QSortFilterProxyModel(parent), m_filter(fw) -{ - connect(&fw, &FilterWidget::changed, [&]{ invalidateFilter(); }); -} - -bool FilterWidgetProxyModel::filterAcceptsRow( - int sourceRow, const QModelIndex& sourceParent) const -{ - const auto cols = sourceModel()->columnCount(); - - const auto m = m_filter.matches([&](auto&& what) { - for (int c=0; c<cols; ++c) { - QModelIndex index = sourceModel()->index(sourceRow, c, sourceParent); - const auto text = sourceModel()->data(index, Qt::DisplayRole).toString(); - - if (text.contains(what, Qt::CaseInsensitive)) { - return true; - } - } - - return false; - }); - - return m; -} - - -FilterWidget::FilterWidget() : - m_edit(nullptr), m_list(nullptr), m_proxy(nullptr), - m_eventFilter(nullptr), m_clear(nullptr) -{ -} - -void FilterWidget::setEdit(QLineEdit* edit) -{ - unhook(); - - m_edit = edit; - - if (!m_edit) { - return; - } - - m_edit->setPlaceholderText(QObject::tr("Filter")); - - createClear(); - hookEvents(); - clear(); -} - -void FilterWidget::setList(QAbstractItemView* list) -{ - m_list = list; - - m_proxy = new FilterWidgetProxyModel(*this); - m_proxy->setSourceModel(m_list->model()); - m_list->setModel(m_proxy); -} - -void FilterWidget::clear() -{ - if (!m_edit) { - return; - } - - m_edit->clear(); -} - -bool FilterWidget::empty() const -{ - return m_text.isEmpty(); -} - -QModelIndex FilterWidget::map(const QModelIndex& index) -{ - if (m_proxy) { - return m_proxy->mapToSource(index); - } else { - log::error("FilterWidget::map() called, but proxy isn't set up"); - return index; - } -} - -void FilterWidget::compile() -{ - m_compiled.clear(); - - const QStringList ORList = [&] { - QString filterCopy = QString(m_text); - filterCopy.replace("||", ";").replace("OR", ";").replace("|", ";"); - return filterCopy.split(";", QString::SkipEmptyParts); - }(); - - // split in ORSegments that internally use AND logic - for (auto& ORSegment : ORList) { - m_compiled.push_back(ORSegment.split(" ", QString::SkipEmptyParts)); - } -} - -bool FilterWidget::matches(std::function<bool (const QString& what)> pred) const -{ - if (m_compiled.isEmpty() || !pred) { - return true; - } - - for (auto& ANDKeywords : m_compiled) { - bool segmentGood = true; - - // check each word in the segment for match, each word needs to be matched - // but it doesn't matter where. - for (auto& currentKeyword : ANDKeywords) { - if (!pred(currentKeyword)) { - segmentGood = false; - } - } - - if (segmentGood) { - // the last AND loop didn't break so the ORSegments is true so mod - // matches filter - return true; - } - } - - return false; -} - -void FilterWidget::unhook() -{ - if (m_clear) { - delete m_clear; - m_clear = nullptr; - } - - if (m_edit) { - m_edit->removeEventFilter(m_eventFilter); - } - - if (m_proxy && m_list) { - auto* model = m_proxy->sourceModel(); - m_proxy->setSourceModel(nullptr); - delete m_proxy; - - m_list->setModel(model); - } -} - -void FilterWidget::createClear() -{ - m_clear = new QToolButton(m_edit); - - QPixmap pixmap(":/MO/gui/edit_clear"); - m_clear->setIcon(QIcon(pixmap)); - m_clear->setIconSize(pixmap.size()); - m_clear->setCursor(Qt::ArrowCursor); - m_clear->setStyleSheet("QToolButton { border: none; padding: 0px; }"); - m_clear->hide(); - - QObject::connect(m_clear, &QToolButton::clicked, [&]{ clear(); }); - QObject::connect(m_edit, &QLineEdit::textChanged, [&]{ onTextChanged(); }); - - repositionClearButton(); -} - -void FilterWidget::hookEvents() -{ - m_eventFilter = new EventFilter(m_edit, [&](auto* w, auto* e) { - if (e->type() == QEvent::Resize) { - onResized(); - } - - return false; - }); - - m_edit->installEventFilter(m_eventFilter); -} - -void FilterWidget::onTextChanged() -{ - m_clear->setVisible(!m_edit->text().isEmpty()); - - const auto text = m_edit->text(); - - if (text != m_text) { - m_text = text; - compile(); - - if (m_proxy) { - m_proxy->invalidateFilter(); - } - - emit changed(); - } -} - -void FilterWidget::onResized() -{ - repositionClearButton(); -} - -void FilterWidget::repositionClearButton() -{ - if (!m_clear) { - return; - } - - const QSize sz = m_clear->sizeHint(); - const int frame = m_edit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - const auto r = m_edit->rect(); - - const auto x = r.right() - frame - sz.width(); - const auto y = (r.bottom() + 1 - sz.height()) / 2; - - m_clear->move(x, y); -} diff --git a/src/filterwidget.h b/src/filterwidget.h deleted file mode 100644 index 4fb9831f..00000000 --- a/src/filterwidget.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef FILTERWIDGET_H -#define FILTERWIDGET_H - -#include <QObject> -#include <QLineEdit> -#include <QToolButton> -#include <QList> -#include <QAbstractItemView> - -class EventFilter; -class FilterWidget; - -class FilterWidgetProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT; - -public: - FilterWidgetProxyModel(FilterWidget& fw, QWidget* parent=nullptr); - using QSortFilterProxyModel::invalidateFilter; - -protected: - bool filterAcceptsRow(int row, const QModelIndex& parent) const override; - -private: - FilterWidget& m_filter; -}; - - -class FilterWidget : public QObject -{ - Q_OBJECT; - -public: - using predFun = std::function<bool (const QString& what)>; - - FilterWidget(); - - void setEdit(QLineEdit* edit); - void setList(QAbstractItemView* list); - void clear(); - bool empty() const; - - QModelIndex map(const QModelIndex& index); - - bool matches(predFun pred) const; - -signals: - void changed(); - -private: - QLineEdit* m_edit; - QAbstractItemView* m_list; - FilterWidgetProxyModel* m_proxy; - EventFilter* m_eventFilter; - QToolButton* m_clear; - QString m_text; - QList<QList<QString>> m_compiled; - - void unhook(); - void createClear(); - void hookEvents(); - void repositionClearButton(); - - void onTextChanged(); - void onResized(); - - void compile(); -}; - -#endif // FILTERWIDGET_H |
