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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "projecttargetscombobox.h"
0008 
0009 #include <QIcon>
0010 
0011 #include <project/projectmodel.h>
0012 #include <interfaces/iproject.h>
0013 #include <util/kdevstringhandler.h>
0014 #include <interfaces/icore.h>
0015 #include <interfaces/iprojectcontroller.h>
0016 
0017 using namespace KDevelop;
0018 
0019 ProjectTargetsComboBox::ProjectTargetsComboBox(QWidget* parent)
0020     : QComboBox(parent)
0021 {
0022 
0023 }
0024     
0025 class ExecutablePathsVisitor
0026     : public ProjectVisitor
0027 {
0028     public:
0029         explicit ExecutablePathsVisitor(bool exec) : m_onlyExecutables(exec) {}
0030         using ProjectVisitor::visit;
0031         void visit(ProjectExecutableTargetItem* eit) override {
0032             if(!m_onlyExecutables || eit->type()==ProjectTargetItem::ExecutableTarget)
0033                 m_paths += KDevelop::joinWithEscaping(eit->model()->pathFromIndex(eit->index()), QLatin1Char('/'), QLatin1Char('\\'));
0034         }
0035 
0036         QStringList paths() const { return m_paths; }
0037 
0038         void sort() { m_paths.sort(); }
0039 
0040     private:
0041         bool m_onlyExecutables;
0042         QStringList m_paths;
0043 };
0044 
0045 
0046 void ProjectTargetsComboBox::setBaseItem(ProjectFolderItem* item, bool exec)
0047 {
0048     clear();
0049 
0050     QList<ProjectFolderItem*> items;
0051     if(item) {
0052         items += item;
0053     } else {
0054         const auto projects = ICore::self()->projectController()->projects();
0055         items.reserve(projects.size());
0056         for (auto* p : projects) {
0057             items += p->projectItem();
0058         }
0059     }
0060 
0061     ExecutablePathsVisitor walker(exec);
0062     for (ProjectFolderItem* item : qAsConst(items)) {
0063         walker.visit(item);
0064     }
0065     walker.sort();
0066 
0067     const auto executableItems = walker.paths();
0068     for (const QString& item : executableItems) {
0069         addItem(QIcon::fromTheme(QStringLiteral("system-run")), item);
0070     }
0071 
0072 }
0073 
0074 QStringList ProjectTargetsComboBox::currentItemPath() const
0075 {
0076     return KDevelop::splitWithEscaping(currentText(), QLatin1Char('/'), QLatin1Char('\\'));
0077 }
0078 
0079 void ProjectTargetsComboBox::setCurrentItemPath(const QStringList& str)
0080 {
0081     setCurrentIndex(str.isEmpty() && count() ? 0 : findText(KDevelop::joinWithEscaping(str, QLatin1Char('/'), QLatin1Char('\\'))));
0082 }
0083 
0084 #include "moc_projecttargetscombobox.cpp"