Commit 68ad2b72 authored by Eric Armbruster's avatar Eric Armbruster 🍁 Committed by Christoph Cullmann
Browse files

Fix recent projects to work with new welcomeview

Also rename recentfiles to recentitems again.
parent 78149a31
Pipeline #247317 passed with stage
in 8 minutes and 45 seconds
......@@ -838,7 +838,7 @@ void KateProjectPluginView::openDirectoryOrProject(const QDir &dir)
if (auto *parentClient = qobject_cast<KXmlGuiWindow *>(m_mainWindow->window())) {
if (auto *openRecentAction = parentClient->action(KStandardAction::name(KStandardAction::StandardAction::OpenRecent))) {
if (auto *recentFilesAction = qobject_cast<KRecentFilesAction *>(openRecentAction)) {
recentFilesAction->addUrl(QUrl(dir.path()));
recentFilesAction->addUrl(QUrl::fromLocalFile(dir.path()));
}
}
}
......
......@@ -120,7 +120,7 @@ target_sources(
filehistorywidget.cpp
welcomeview/welcomeview.cpp
welcomeview/recentfilesmodel.cpp
welcomeview/recentitemsmodel.cpp
welcomeview/savedsessionsmodel.cpp
)
......
......@@ -292,15 +292,7 @@ void KateMainWindow::setupActions()
m_fileOpenRecent = KStandardAction::openRecent(
m_viewManager,
[this](const QUrl &url) {
if (url.isLocalFile()) {
QDir dir(url.path());
if (dir.exists()) {
Utils::openDirectoryOrProject(this, dir);
return;
}
}
openUrl(url);
Utils::openUrlOrProject(viewManager(), url);
},
this);
m_fileOpenRecent->setMaxItems(KateConfigDialog::recentFilesMaxCount());
......
......@@ -6,6 +6,7 @@
#include "ktexteditor_utils.h"
#include "katemainwindow.h"
#include "kateviewmanager.h"
#include <QDir>
#include <QFontDatabase>
......@@ -193,12 +194,23 @@ QVariantMap projectMapForDocument(KTextEditor::Document *doc)
return projectMap;
}
void openDirectoryOrProject(KateMainWindow *mainWindow, const QDir &dir)
void openUrlOrProject(KateViewManager *viewManager, const QUrl &url)
{
if (QObject *pview = mainWindow->pluginView(QStringLiteral("kateprojectplugin"))) {
if (!url.isLocalFile()) {
viewManager->openUrl(url);
return;
}
QDir dir = {url.toLocalFile()};
if (!dir.exists()) {
viewManager->openUrl(url);
return;
}
if (QObject *pview = viewManager->mainWindow()->pluginView(QStringLiteral("kateprojectplugin"))) {
QMetaObject::invokeMethod(pview, "openDirectoryOrProject", Q_ARG(const QDir &, dir));
} else {
KMessageBox::error(mainWindow, i18n("Please enable the project plugin to load directories"));
KMessageBox::error(viewManager->mainWindow(), i18n("Please enable the project plugin to load directories"));
}
}
}
......@@ -10,11 +10,11 @@
QT_BEGIN_NAMESPACE
class QScrollBar;
class QAction;
class QDir;
class QFont;
class QIcon;
class QVariant;
class QWidget;
class QUrl;
typedef QMap<QString, QVariant> QVariantMap;
QT_END_NAMESPACE
......@@ -25,7 +25,7 @@ class Document;
class MainWindow;
}
struct DiffParams;
class KateMainWindow;
class KateViewManager;
namespace Utils
{
......@@ -96,5 +96,5 @@ KATE_PRIVATE_EXPORT QVariantMap projectMapForDocument(KTextEditor::Document *doc
*
* Returns whether the url is a directory
*/
KATE_PRIVATE_EXPORT void openDirectoryOrProject(KateMainWindow *mainWindow, const QDir &dir);
KATE_PRIVATE_EXPORT void openUrlOrProject(KateViewManager *viewManager, const QUrl &url);
}
......@@ -5,30 +5,30 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "recentfilesmodel.h"
#include "kateconfigdialog.h"
#include "recentitemsmodel.h"
#include <QDebug>
#include <QFileInfo>
#include <QMimeDatabase>
RecentFilesModel::RecentFilesModel(QObject *parent)
RecentItemsModel::RecentItemsModel(QObject *parent)
: QAbstractListModel(parent)
{}
{
}
QVariant RecentFilesModel::data(const QModelIndex &index, int role) const
QVariant RecentItemsModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
const int row = index.row();
if (row >= 0 && row < m_recentFiles.count()) {
const RecentFileInfo &recentFile = m_recentFiles.at(row);
if (row >= 0 && row < m_recentItems.count()) {
const RecentItemInfo &recentItem = m_recentItems.at(row);
switch (role) {
case Qt::DisplayRole:
return recentFile.name;
return recentItem.name;
case Qt::DecorationRole:
return recentFile.icon;
return recentItem.icon;
case Qt::ToolTipRole:
return recentFile.url.toString(QUrl::PreferLocalFile);
return recentItem.url.toString(QUrl::PreferLocalFile);
default:
break;
}
......@@ -38,22 +38,23 @@ QVariant RecentFilesModel::data(const QModelIndex &index, int role) const
return QVariant();
}
int RecentFilesModel::rowCount(const QModelIndex &parent) const
int RecentItemsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_recentFiles.count();
return m_recentItems.count();
}
void RecentFilesModel::refresh(const QList<QUrl> &urls)
void RecentItemsModel::refresh(const QList<QUrl> &urls)
{
QVector<RecentFileInfo> recentFiles;
recentFiles.reserve(urls.count());
QVector<RecentItemInfo> recentItems;
recentItems.reserve(urls.count());
QIcon icon;
QString name;
for (const QUrl &url: urls) {
for (const QUrl &url : urls) {
if (url.isLocalFile()) {
qDebug() << url << " is local file";
const QFileInfo fileInfo(url.toLocalFile());
if (!fileInfo.exists()) {
continue;
......@@ -62,24 +63,25 @@ void RecentFilesModel::refresh(const QList<QUrl> &urls)
icon = QIcon::fromTheme(QMimeDatabase().mimeTypeForFile(fileInfo).iconName());
name = fileInfo.fileName();
} else {
qDebug() << url << " is not local file";
icon = QIcon::fromTheme(QStringLiteral("network-server"));
name = url.toString();
}
recentFiles.append({ icon, name, url });
recentItems.append({icon, name, url});
}
beginResetModel();
m_recentFiles = std::move(recentFiles);
m_recentItems = std::move(recentItems);
endResetModel();
}
QUrl RecentFilesModel::url(const QModelIndex &index) const
QUrl RecentItemsModel::url(const QModelIndex &index) const
{
if (index.isValid()) {
const int row = index.row();
if (row >= 0 && row < m_recentFiles.count()) {
return m_recentFiles.at(row).url;
if (row >= 0 && row < m_recentItems.count()) {
return m_recentItems.at(row).url;
}
}
......
......@@ -12,12 +12,12 @@
#include <QIcon>
#include <QUrl>
class RecentFilesModel : public QAbstractListModel
class RecentItemsModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit RecentFilesModel(QObject *parent = nullptr);
explicit RecentItemsModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
......@@ -26,13 +26,13 @@ public:
QUrl url(const QModelIndex &index) const;
private:
struct RecentFileInfo {
struct RecentItemInfo {
QIcon icon;
QString name;
QUrl url;
};
QVector<RecentFileInfo> m_recentFiles;
QVector<RecentItemInfo> m_recentItems;
};
#endif // RECENTFILESMODEL_H
......@@ -10,7 +10,7 @@
#include "kateapp.h"
#include "kateviewmanager.h"
#include "ktexteditor_utils.h"
#include "recentfilesmodel.h"
#include "recentitemsmodel.h"
#include "savedsessionsmodel.h"
#include <KAboutData>
......@@ -64,35 +64,27 @@ WelcomeView::WelcomeView(KateViewManager *viewManager, QWidget *parent)
layoutPlaceholderRecentFiles->addWidget(placeholderRecentFiles);
listViewRecentFiles->setLayout(layoutPlaceholderRecentFiles);
m_recentFilesModel = new RecentFilesModel(this);
connect(m_recentFilesModel, &RecentFilesModel::modelReset, this, [this, placeholderRecentFiles]() {
const bool noRecentFiles = m_recentFilesModel->rowCount() == 0;
m_recentItemsModel = new RecentItemsModel(this);
connect(m_recentItemsModel, &RecentItemsModel::modelReset, this, [this, placeholderRecentFiles]() {
const bool noRecentFiles = m_recentItemsModel->rowCount() == 0;
buttonClearRecentFiles->setDisabled(noRecentFiles);
placeholderRecentFiles->setVisible(noRecentFiles);
});
KRecentFilesAction *recentFilesAction = m_viewManager->mainWindow()->recentFilesAction();
m_recentFilesModel->refresh(recentFilesAction->urls());
m_recentItemsModel->refresh(recentFilesAction->urls());
connect(recentFilesAction, &KRecentFilesAction::recentListCleared, this, [this, recentFilesAction]() {
m_recentFilesModel->refresh(recentFilesAction->urls());
m_recentItemsModel->refresh(recentFilesAction->urls());
});
listViewRecentFiles->setModel(m_recentFilesModel);
listViewRecentFiles->setModel(m_recentItemsModel);
connect(listViewRecentFiles, &QListView::customContextMenuRequested,
this, &WelcomeView::onRecentFilesContextMenuRequested);
connect(listViewRecentFiles, &QListView::activated, this, [this](const QModelIndex &index) {
if (index.isValid()) {
const QUrl url = m_recentFilesModel->url(index);
const QUrl url = m_recentItemsModel->url(index);
Q_ASSERT(url.isValid());
if (url.isLocalFile()) {
QDir dir = {url.path()};
if (dir.exists()) {
Utils::openDirectoryOrProject(m_viewManager->mainWindow(), dir);
return;
}
}
m_viewManager->openUrl(url);
Utils::openUrlOrProject(m_viewManager, url);
}
});
......@@ -201,8 +193,10 @@ void WelcomeView::onPluginViewChanged(const QString &pluginName)
if (projectPluginView) {
connect(buttonOpenFolder, SIGNAL(clicked()), projectPluginView, SLOT(openDirectoryOrProject()));
buttonOpenFolder->show();
labelRecentItems->setText(i18n("Recent Documents and Projects"));
} else {
buttonOpenFolder->hide();
labelRecentItems->setText(i18n("Recent Documents"));
}
}
}
......@@ -214,7 +208,7 @@ void WelcomeView::onRecentFilesContextMenuRequested(const QPoint &pos)
return;
}
const QUrl url = m_recentFilesModel->url(index);
const QUrl url = m_recentItemsModel->url(index);
Q_ASSERT(url.isValid());
QMenu contextMenu;
......@@ -239,7 +233,7 @@ void WelcomeView::onRecentFilesContextMenuRequested(const QPoint &pos)
connect(action, &QAction::triggered, this, [this, url]() {
KRecentFilesAction *recentFilesAction = m_viewManager->mainWindow()->recentFilesAction();
recentFilesAction->removeUrl(url);
m_recentFilesModel->refresh(recentFilesAction->urls());
m_recentItemsModel->refresh(recentFilesAction->urls());
});
contextMenu.addAction(action);
......
......@@ -11,7 +11,7 @@
#include "ui_welcomeview.h"
class KateViewManager;
class RecentFilesModel;
class RecentItemsModel;
class SavedSessionsModel;
class WelcomeView : public QScrollArea, Ui::WelcomeView
......@@ -33,7 +33,7 @@ private:
bool updateLayout();
KateViewManager *m_viewManager = nullptr;
RecentFilesModel *m_recentFilesModel = nullptr;
RecentItemsModel *m_recentItemsModel = nullptr;
SavedSessionsModel *m_savedSessionsModel = nullptr;
};
......
......@@ -177,8 +177,8 @@
</widget>
</item>
<item>
<widget class="QWidget" name="widgetRecentFiles" native="true">
<layout class="QGridLayout" name="layoutRecentFiles" rowstretch="0,0,0,0,1" columnstretch="0,1,0">
<widget class="QWidget" name="widgetRecentItems" native="true">
<layout class="QGridLayout" name="layoutRecentItems" rowstretch="0,0,0,0,1" columnstretch="0,1,0">
<property name="leftMargin">
<number>0</number>
</property>
......@@ -209,14 +209,14 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="labelRecentFiles">
<widget class="QLabel" name="labelRecentItems">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Recent Files</string>
<string>Recent Documents</string>
</property>
</widget>
</item>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment