File indexing completed on 2023-12-03 04:55:10

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Julius K├╝nzel <jk.kdedev@smartlab.uber.space>
0003  * SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004  */
0005 #include "markercategorychooser.h"
0006 #include "../bin/model/markerlistmodel.hpp"
0007 #include "core.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 MarkerCategoryChooser::MarkerCategoryChooser(QWidget *parent)
0012     : QComboBox(parent)
0013     , m_markerListModel(nullptr)
0014     , m_allowAll(true)
0015     , m_onlyUsed(false)
0016 {
0017     refresh();
0018     connect(this, &MarkerCategoryChooser::changed, this, &MarkerCategoryChooser::refresh);
0019 }
0020 
0021 void MarkerCategoryChooser::refresh()
0022 {
0023     clear();
0024     if (pCore == nullptr) {
0025         return;
0026     }
0027     // Set up guide categories
0028     QPixmap pixmap(32, 32);
0029     QMapIterator<int, Core::MarkerCategory> i(pCore->markerTypes);
0030     while (i.hasNext()) {
0031         i.next();
0032         if (m_onlyUsed && m_markerListModel && m_markerListModel->getAllMarkers(i.key()).isEmpty()) {
0033             continue;
0034         }
0035         pixmap.fill(i.value().color);
0036         QIcon colorIcon(pixmap);
0037         addItem(colorIcon, i.value().displayName, i.key());
0038     }
0039     if (count() == 0) {
0040         setEnabled(false);
0041         setPlaceholderText(i18n("Nothing to select"));
0042         return;
0043     }
0044     setEnabled(true);
0045     setPlaceholderText(QString());
0046     if (m_allowAll) {
0047         insertItem(0, i18n("All Categories"), -1);
0048         setCurrentIndex(0);
0049     }
0050 }
0051 
0052 void MarkerCategoryChooser::setCurrentCategory(int category)
0053 {
0054     setCurrentIndex(findData(category));
0055 }
0056 
0057 int MarkerCategoryChooser::currentCategory()
0058 {
0059     return currentData().toInt();
0060 }
0061 
0062 void MarkerCategoryChooser::setMarkerModel(const MarkerListModel *model)
0063 {
0064     m_markerListModel = model;
0065     Q_EMIT changed();
0066 }
0067 
0068 void MarkerCategoryChooser::setAllowAll(bool allowAll)
0069 {
0070     m_allowAll = allowAll;
0071     Q_EMIT changed();
0072 }
0073 
0074 void MarkerCategoryChooser::setOnlyUsed(bool onlyUsed)
0075 {
0076     m_onlyUsed = onlyUsed;
0077     Q_EMIT changed();
0078 }