File indexing completed on 2024-04-28 04:38:37

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "documentswitcheritem.h"
0008 #include "documentswitchertreeview.h"
0009 
0010 #include <KFileItem>
0011 
0012 #include <QDir>
0013 
0014 #include <interfaces/icore.h>
0015 #include <interfaces/idocument.h>
0016 #include <interfaces/iproject.h>
0017 #include <interfaces/iprojectcontroller.h>
0018 
0019 DocumentSwitcherItem::DocumentSwitcherItem(KDevelop::IDocument *document)
0020 {
0021     const QUrl &url = document->url();
0022 
0023     KDevelop::IProjectController *projectController = KDevelop::ICore::self()->projectController();
0024 
0025     // Find file icon : file type icon or document unsaved icon.
0026     switch (document->state())
0027     {
0028         case KDevelop::IDocument::Clean:
0029         {
0030             setIcon(QIcon::fromTheme(KFileItem(url, QString(), 0).iconName()));
0031             break;
0032         }
0033         case KDevelop::IDocument::Modified:
0034         {
0035             setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
0036             break;
0037         }
0038         case KDevelop::IDocument::Dirty:
0039         {
0040             setIcon(QIcon::fromTheme(QStringLiteral("document-revert")));
0041             break;
0042         }
0043         case KDevelop::IDocument::DirtyAndModified:
0044         {
0045             setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
0046             break;
0047         }
0048     }
0049 
0050     // Extract file name and path.
0051     QString text = url.fileName();
0052     QString path = projectController->prettyFilePath(url, KDevelop::IProjectController::FormatPlain);
0053 
0054     const bool isPartOfOpenProject = QDir::isRelativePath(path);
0055     if (path.endsWith(QLatin1Char('/'))) {
0056         path.chop(1);
0057     }
0058     if (isPartOfOpenProject) {
0059         const int projectNameSize = path.indexOf(QLatin1Char(':'));
0060 
0061         // first: project name, second: path to file in project (might be just '/' when the file is in the project root dir)
0062         const QPair<QString, QString> fileInProjectInfo = (projectNameSize < 0)
0063             ? qMakePair(path, QStringLiteral("/"))
0064             : qMakePair(path.left(projectNameSize), path.mid(projectNameSize + 1));
0065 
0066         text = QStringLiteral("%1 (%2:%3)").arg(text, fileInProjectInfo.first, fileInProjectInfo.second);
0067     }
0068     else {
0069         text += QLatin1String(" (") + path + QLatin1Char(')');
0070     }
0071 
0072     setText(text);
0073 
0074     // Set item data.
0075     KDevelop::IProject *project = projectController->findProjectForUrl(url);
0076     setData(QVariant::fromValue(project), DocumentSwitcherTreeView::ProjectRole);
0077 }
0078 
0079 DocumentSwitcherItem::~DocumentSwitcherItem() = default;
0080