Commit 09ca221c authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

allow to have links to sub projects inside projects

parent 5371ccf6
......@@ -78,16 +78,20 @@ QVariant KateProjectItem::data(int role) const
return QVariant(*icon());
}
if (role == TypeRole) {
return QVariant(m_type);
}
return QStandardItem::data(role);
}
bool KateProjectItem::operator<(const QStandardItem &other) const
{
// let directories stay first
const bool isDirectory = data(Qt::ToolTipRole).isNull();
const bool otherIsDirectory = other.data(Qt::ToolTipRole).isNull();
if (isDirectory != otherIsDirectory) {
return isDirectory > otherIsDirectory;
const auto thisType = data(TypeRole).toInt();
const bool otherType = other.data(TypeRole).toInt();
if (thisType != otherType) {
return thisType < otherType;
}
// case-insensitive compare of the filename
......@@ -101,6 +105,7 @@ QIcon *KateProjectItem::icon() const
}
switch (m_type) {
case LinkedProject:
case Project:
m_icon = new QIcon(QIcon::fromTheme(QStringLiteral("folder-documents")));
break;
......
......@@ -26,8 +26,14 @@ class KateProjectItem : public QStandardItem
public:
/**
* Possible Types
* We start with 1 to have 0 as invalid value!
*/
enum Type { Project, Directory, File };
enum Type { LinkedProject = 1, Project = 2, Directory = 3, File = 4 };
/**
* Our defined roles
*/
enum Role { TypeRole = Qt::UserRole + 42 };
/**
* construct new item with given text
......
......@@ -405,6 +405,29 @@ void KateProjectPluginView::slotDocumentUrlChanged(KTextEditor::Document *docume
}
}
void KateProjectPluginView::switchToProject(const QDir &dir)
{
/**
* search matching project
*/
KateProject *project = m_plugin->projectForDir(dir);
if (!project) {
return;
}
/**
* get active project view and switch it, if it is for a different project
* do this AFTER file selection
*/
KateProjectView *active = static_cast<KateProjectView *>(m_stackedProjectViews->currentWidget());
if (active != m_project2View.value(project).first) {
int index = m_projectsCombo->findData(project->fileName());
if (index >= 0) {
m_projectsCombo->setCurrentIndex(index);
}
}
}
void KateProjectPluginView::slotViewCreated(KTextEditor::View *view)
{
/**
......
......@@ -107,6 +107,12 @@ public Q_SLOTS:
*/
QPair<KateProjectView *, KateProjectInfoView *> viewForProject(KateProject *project);
/**
* Switch to project for given dir, if any there!
* @param dir dir with the project
*/
void switchToProject(const QDir &dir);
private Q_SLOTS:
/**
* Plugin config updated
......
......@@ -92,6 +92,13 @@ void KateProjectViewTree::openSelectedDocument()
return;
}
/**
* we only handle files here!
*/
if (selecteStuff[0].data(KateProjectItem::TypeRole).toInt() != KateProjectItem::File) {
return;
}
/**
* open document for first element, if possible
*/
......@@ -106,10 +113,23 @@ void KateProjectViewTree::slotClicked(const QModelIndex &index)
/**
* open document, if any usable user data
*/
QString filePath = index.data(Qt::UserRole).toString();
const QString filePath = index.data(Qt::UserRole).toString();
if (!filePath.isEmpty()) {
m_pluginView->mainWindow()->openUrl(QUrl::fromLocalFile(filePath));
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear | QItemSelectionModel::Select);
/**
* normal file?
*/
if (index.data(KateProjectItem::TypeRole).toInt() == KateProjectItem::File) {
m_pluginView->mainWindow()->openUrl(QUrl::fromLocalFile(filePath));
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear | QItemSelectionModel::Select);
}
/**
* we might be a linked project => trigger that we switch to the selected one!
*/
if (index.data(KateProjectItem::TypeRole).toInt() == KateProjectItem::LinkedProject) {
m_pluginView->switchToProject(QDir(filePath));
return;
}
}
}
......
......@@ -94,12 +94,12 @@ void KateProjectWorker::loadProject(QStandardItem *parent, const QVariantMap &pr
/**
* recurse to sub-projects FIRST
*/
QVariantList subGroups = project[QStringLiteral("projects")].toList();
const QVariantList subGroups = project[QStringLiteral("projects")].toList();
for (const QVariant &subGroupVariant : subGroups) {
/**
* convert to map and get name, else skip
*/
QVariantMap subProject = subGroupVariant.toMap();
const QVariantMap subProject = subGroupVariant.toMap();
const QString keyName = QStringLiteral("name");
if (subProject[keyName].toString().isEmpty()) {
continue;
......@@ -117,7 +117,7 @@ void KateProjectWorker::loadProject(QStandardItem *parent, const QVariantMap &pr
* load all specified files
*/
const QString keyFiles = QStringLiteral("files");
QVariantList files = project[keyFiles].toList();
const QVariantList files = project[keyFiles].toList();
for (const QVariant &fileVariant : files) {
loadFilesEntry(parent, fileVariant.toMap(), file2Item);
}
......@@ -191,6 +191,51 @@ void KateProjectWorker::loadFilesEntry(QStandardItem *parent, const QVariantMap
return;
}
/**
* handle linked projects, if any
* one can reference other projects by specifying the path to them
*/
QStringList linkedProjects = filesEntry[QStringLiteral("projects")].toStringList();
if (!linkedProjects.empty()) {
/**
* ensure project files are made absolute in respect to correct base dir
*/
for (auto &project : linkedProjects) {
project = dir.absoluteFilePath(project);
}
/**
* users might have specified duplicates, this can't happen for the other ways
*/
linkedProjects.removeDuplicates();
/**
* filter out all directories that have no .kateproject inside!
*/
linkedProjects.erase(std::remove_if(linkedProjects.begin(),
linkedProjects.end(),
[](const QString &item) {
const QFileInfo projectFile(item + QLatin1String("/.kateproject"));
return !projectFile.exists() || !projectFile.isFile();
}),
linkedProjects.end());
/**
* now add our projects to the current item parent
* later the tree view will e.g. allow to jump to the sub-projects
*/
for (const auto &project : linkedProjects) {
QStandardItem *subProjectItem = new KateProjectItem(KateProjectItem::LinkedProject, dir.relativeFilePath(project));
subProjectItem->setData(project, Qt::UserRole);
parent->appendRow(subProjectItem);
}
/**
* files with linked projects will ignore all other stuff inside
*/
return;
}
/**
* get list of files for this directory, might query the VCS
*/
......
Markdown is supported
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