diff --git a/addons/tabswitcher/tabswitchertreeview.cpp b/addons/tabswitcher/tabswitchertreeview.cpp index 2fadf8d58..aedbeb10f 100644 --- a/addons/tabswitcher/tabswitchertreeview.cpp +++ b/addons/tabswitcher/tabswitchertreeview.cpp @@ -1,79 +1,77 @@ /* This file is part of the KDE project Copyright (C) 2014 Dominik Haumann This library 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 library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "tabswitchertreeview.h" #include "tabswitcher.h" #include #include TabSwitcherTreeView::TabSwitcherTreeView() : QTreeView() { setWindowFlags(Qt::Popup | Qt::FramelessWindowHint); setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection); //setUniformItemSizes(true); setTextElideMode(Qt::ElideMiddle); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHeaderHidden(true); setRootIsDecorated(false); } int TabSwitcherTreeView::sizeHintWidth() const { return sizeHintForColumn(0) + sizeHintForColumn(1); } void TabSwitcherTreeView::resizeColumnsToContents() { - // TODO: does not work properly (or at all?) - qDebug() << "resizeColumnsToContents()"; resizeColumnToContents(0); resizeColumnToContents(1); } void TabSwitcherTreeView::keyReleaseEvent(QKeyEvent * event) { if (event->key() == Qt::Key_Control) { emit itemActivated(selectionModel()->currentIndex()); event->accept(); hide(); } else { QTreeView::keyReleaseEvent(event); } } void TabSwitcherTreeView::keyPressEvent(QKeyEvent * event) { if (event->key() == Qt::Key_Escape) { event->accept(); hide(); } else { QTreeView::keyPressEvent(event); } } void TabSwitcherTreeView::showEvent(QShowEvent* event) { resizeColumnsToContents(); QTreeView::showEvent(event); } diff --git a/addons/tabswitcher/tabswitchertreeview.h b/addons/tabswitcher/tabswitchertreeview.h index 5ea7e689b..52548f6f1 100644 --- a/addons/tabswitcher/tabswitchertreeview.h +++ b/addons/tabswitcher/tabswitchertreeview.h @@ -1,73 +1,73 @@ /* This file is part of the KDE project Copyright (C) 2014 Dominik Haumann This library 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 library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KTEXTEDITOR_TABSWITCHER_TREEVIEW_H #define KTEXTEDITOR_TABSWITCHER_TREEVIEW_H #include /** * TODO: see screenshots https://phabricator.kde.org/D16054: * some paths are truncated on the right side. Why? */ class TabSwitcherTreeView : public QTreeView { Q_OBJECT public: /** * Default constructor */ TabSwitcherTreeView(); - int sizeHintWidth() const; - /** - * todo: see inside + * Sum of the widths of both columns */ + int sizeHintWidth() const; + void resizeColumnsToContents(); Q_SIGNALS: /** * This signal is emitted whenever use activates an item through * the list view. * @note @p selectionIndex is a model index of the selectionModel() * and not of the QListView's model itself. */ void itemActivated(const QModelIndex & selectionIndex); protected: /** * Reimplemented for tracking the CTRL key modifier. */ void keyReleaseEvent(QKeyEvent * event) override; /** * Reimplemented for tracking the ESCAPE key. */ void keyPressEvent(QKeyEvent * event) override; /** - * Reimplemented for adjusting the column widths (todo: does not work yet) + * Reimplemented for adjusting the column widths to fit the contents */ void showEvent(QShowEvent *event) override; }; #endif // KTEXTEDITOR_TABSWITCHER_TREEVIEW_H diff --git a/addons/tabswitcher/tests/CMakeLists.txt b/addons/tabswitcher/tests/CMakeLists.txt index f54d7234a..699cec17c 100644 --- a/addons/tabswitcher/tests/CMakeLists.txt +++ b/addons/tabswitcher/tests/CMakeLists.txt @@ -1,20 +1,19 @@ cmake_minimum_required(VERSION 3.0.0) project(tstestapp) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed. set(CMAKE_AUTOMOC ON) # Find the QtWidgets library find_package(Qt5Widgets CONFIG REQUIRED) set(SRC - main.cpp tstestapp.cpp ../tabswitcherfilesmodel.cpp ) add_executable(tstestapp ${SRC}) target_link_libraries(tstestapp Qt5::Widgets) diff --git a/addons/tabswitcher/tests/main.cpp b/addons/tabswitcher/tests/main.cpp deleted file mode 100644 index 415be34ee..000000000 --- a/addons/tabswitcher/tests/main.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "tstestapp.h" -#include - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - TsTestApp w; - w.show(); - - return app.exec(); -} diff --git a/addons/tabswitcher/tests/tstestapp.cpp b/addons/tabswitcher/tests/tstestapp.cpp index 2028a4bdb..97fd0194d 100644 --- a/addons/tabswitcher/tests/tstestapp.cpp +++ b/addons/tabswitcher/tests/tstestapp.cpp @@ -1,112 +1,122 @@ #include "tstestapp.h" #include "../tabswitcherfilesmodel.h" +#include #include #include #include #include #include #include #include #include #include #include #include // https://www.fluentcpp.com/2017/09/22/make-pimpl-using-unique_ptr/ class TsTestApp::Impl { public: void insert_1_item() { auto icon = QIcon::fromTheme(QLatin1String("blurimage")); model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("abc.d"), QLatin1String("/home/user2/folder1/abc.d"))); treeview1->resizeColumnToContents(0); } void remove_1_item() { model.removeRow(0); treeview1->resizeColumnToContents(0); } void set_items_cutoff_bug() { model.clear(); auto icon = QIcon::fromTheme(QLatin1String("document-export")); #define INS__(a, b) model.insertRow(model.rowCount(), new detail::FilenameListItem(icon, QLatin1String(a), QLatin1String(b))); INS__("multimedia-system.log", "/home/gregor/logs/notifications/multimedia-system.log") INS__("servicemenueditor", "/home/gregor/dev/src/kservicemenueditor-0.2a/servicemenueditor") INS__("kdesrc-build", "/home/gregor/kde/src/kdesrc-build/kdesrc-build") INS__("README.md (3)", "/home/gregor/node_modules/autolinker/README.md") INS__("package.json (3)", "/home/gregor/node_modules/autolinker/package.json") INS__("LICENSE (3)", "/home/gregor/node_modules/autolinker/LICENSE") INS__("package.json (2)", "/home/gregor/node_modules/asynckit/package.json") treeview1->resizeColumnToContents(0); } public: detail::TabswitcherFilesModel model; QTreeView* treeview1; }; TsTestApp::TsTestApp(QWidget *parent) : QMainWindow(parent), impl_(new TsTestApp::Impl) { setGeometry(0, 0, 1024, 800); setCentralWidget(new QWidget(this)); auto l = new QVBoxLayout(); centralWidget()->setLayout(l); auto hl = new QHBoxLayout(); l->addLayout(hl); auto buttonInsert1 = new QPushButton(QLatin1String("Ins 1 item"), this); connect(buttonInsert1, &QPushButton::clicked, this, [=] { impl_->insert_1_item(); }); hl->addWidget(buttonInsert1); auto buttonRemove1 = new QPushButton(QLatin1String("Del 1 item"), this); connect(buttonRemove1, &QPushButton::clicked, this, [=] { impl_->remove_1_item(); }); hl->addWidget(buttonRemove1); auto buttonSetTestSet1 = new QPushButton(QLatin1String("set_items_cutoff_bug"), this); connect(buttonSetTestSet1, &QPushButton::clicked, this, [=] { impl_->set_items_cutoff_bug(); }); hl->addWidget(buttonSetTestSet1); impl_->treeview1 = new QTreeView(this); l->addWidget(impl_->treeview1); impl_->treeview1->setHeaderHidden(true); impl_->treeview1->setRootIsDecorated(false); auto icon = QIcon::fromTheme(QLatin1String("edit-undo")); impl_->model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("file1.h"), QLatin1String("/home/gm/projects/proj1/src/file1.h"))); impl_->model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("file2.cpp"), QLatin1String("/home/gm/projects/proj1/src/file2.cpp"))); impl_->model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("file3.py"), QLatin1String("/home/gm/dev/file3.py"))); impl_->model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("file3kjaskdfkljasdfklj089asdfkjklasdjf90asdfsdfkj.py"), QLatin1String("/home/gm/dev/file3kjaskdfkljasdfklj089asdfkjklasdjf90asdfsdfkj.py"))); impl_->model.insertRow(0, new detail::FilenameListItem(icon, QLatin1String("file3.py"), QLatin1String("/home/gm/dev/proj2/asldfkjasdfk/asdlfkjasd;faf/;ajsdkfgjaskdfgasdf/file3.py"))); //impl_->insert_a_item(); //impl_->remove_a_item(); impl_->model.rowCount(); impl_->model.item(0); impl_->model.index(0, 0); impl_->treeview1->setModel(&impl_->model); impl_->treeview1->resizeColumnToContents(0); impl_->treeview1->resizeColumnToContents(1); auto listview1 = new QListView(this); l->addWidget(listview1); listview1->setModel(&impl_->model); auto treeview2 = new QTreeView(this); l->addWidget(treeview2); } TsTestApp::~TsTestApp() { } + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + TsTestApp w; + w.show(); + + return app.exec(); +}