mirror of
				https://github.com/arcan1s/awesome-widgets.git
				synced 2025-10-31 21:23:41 +00:00 
			
		
		
		
	initial import of custom keys support (#101)
It will use the configuration file `$AWHOME/custom.ini`, group `[Custom]`. One custom key per string, key is custom key, value is any of available standard data. Formatters will be defined by using standard way.
This commit is contained in:
		
							
								
								
									
										210
									
								
								sources/awesome-widget/plugin/awcustomkeys.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										210
									
								
								sources/awesome-widget/plugin/awcustomkeys.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,210 @@ | |||||||
|  | /*************************************************************************** | ||||||
|  |  *   This file is part of awesome-widgets                                  * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   awesome-widgets is free software: you can redistribute it and/or      * | ||||||
|  |  *   modify it under the terms of the GNU General Public License as        * | ||||||
|  |  *   published by the Free Software Foundation, either version 3 of the    * | ||||||
|  |  *   License, or (at your option) any later version.                       * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   awesome-widgets is distributed in the hope that it will be useful,    * | ||||||
|  |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * | ||||||
|  |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * | ||||||
|  |  *   GNU General Public License for more details.                          * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   You should have received a copy of the GNU General Public License     * | ||||||
|  |  *   along with awesome-widgets. If not, see http://www.gnu.org/licenses/  * | ||||||
|  |  ***************************************************************************/ | ||||||
|  |  | ||||||
|  | #include "awcustomkeys.h" | ||||||
|  | #include "ui_awcustomkeys.h" | ||||||
|  |  | ||||||
|  | #include <QSet> | ||||||
|  | #include <QSettings> | ||||||
|  | #include <QStandardPaths> | ||||||
|  |  | ||||||
|  | #include "awabstractselector.h" | ||||||
|  | #include "awdebug.h" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | AWCustomKeys::AWCustomKeys(QWidget *parent, const QStringList sources) | ||||||
|  |     : QDialog(parent) | ||||||
|  |     , ui(new Ui::AWCustomKeys) | ||||||
|  |     , m_sources(sources) | ||||||
|  | { | ||||||
|  |     qCDebug(LOG_AW) << __PRETTY_FUNCTION__; | ||||||
|  |  | ||||||
|  |     m_filePath = QString("awesomewidgets/custom.ini"); | ||||||
|  |     ui->setupUi(this); | ||||||
|  |     initKeys(); | ||||||
|  |  | ||||||
|  |     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept())); | ||||||
|  |     connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject())); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | AWCustomKeys::~AWCustomKeys() | ||||||
|  | { | ||||||
|  |     qCDebug(LOG_AW) << __PRETTY_FUNCTION__; | ||||||
|  |  | ||||||
|  |     clearSelectors(); | ||||||
|  |     delete ui; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | QStringList AWCustomKeys::customKeys() const | ||||||
|  | { | ||||||
|  |     return m_keys.keys(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::showDialog() | ||||||
|  | { | ||||||
|  |     // update dialog | ||||||
|  |     updateDialog(); | ||||||
|  |     // exec dialog | ||||||
|  |     return execDialog(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | QVariant AWCustomKeys::valueForKey(const QString &key, const QVariantHash &data) | ||||||
|  | { | ||||||
|  |     qCDebug(LOG_AW) << "Find value for key" << key << "in data"; | ||||||
|  |  | ||||||
|  |     return data[m_keys[key]]; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | QStringList AWCustomKeys::usedSources() const | ||||||
|  | { | ||||||
|  |     return QSet<QString>::fromList(m_keys.values()).toList(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::updateUi() | ||||||
|  | { | ||||||
|  |     QPair<QString, QString> current | ||||||
|  |         = static_cast<AWAbstractSelector *>(sender())->current(); | ||||||
|  |     int index | ||||||
|  |         = m_selectors.indexOf(static_cast<AWAbstractSelector *>(sender())); | ||||||
|  |  | ||||||
|  |     if ((current.first.isEmpty()) && (current.second.isEmpty())) { | ||||||
|  |         // remove current selector if it is empty and does not last | ||||||
|  |         if (sender() == m_selectors.last()) | ||||||
|  |             return; | ||||||
|  |         AWAbstractSelector *selector = m_selectors.takeAt(index); | ||||||
|  |         ui->verticalLayout->removeWidget(selector); | ||||||
|  |         selector->deleteLater(); | ||||||
|  |     } else { | ||||||
|  |         // add new selector if something changed | ||||||
|  |         if (sender() != m_selectors.last()) | ||||||
|  |             return; | ||||||
|  |         addSelector(QPair<QString, QString>()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::addSelector(const QPair<QString, QString> ¤t) | ||||||
|  | { | ||||||
|  |     qCDebug(LOG_AW) << "Add selector with current values" << current; | ||||||
|  |  | ||||||
|  |     AWAbstractSelector *selector | ||||||
|  |         = new AWAbstractSelector(ui->scrollAreaWidgetContents, {true, false}); | ||||||
|  |     selector->init(QStringList(), m_sources, current); | ||||||
|  |     ui->verticalLayout->insertWidget(ui->verticalLayout->count() - 1, selector); | ||||||
|  |     connect(selector, SIGNAL(selectionChanged()), this, SLOT(updateUi())); | ||||||
|  |     m_selectors.append(selector); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::clearSelectors() | ||||||
|  | { | ||||||
|  |     for (auto selector : m_selectors) { | ||||||
|  |         disconnect(selector, SIGNAL(selectionChanged()), this, | ||||||
|  |                    SLOT(updateUi())); | ||||||
|  |         ui->verticalLayout->removeWidget(selector); | ||||||
|  |         selector->deleteLater(); | ||||||
|  |     } | ||||||
|  |     m_selectors.clear(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::execDialog() | ||||||
|  | { | ||||||
|  |     int ret = exec(); | ||||||
|  |     m_keys.clear(); | ||||||
|  |     for (auto selector : m_selectors) { | ||||||
|  |         QPair<QString, QString> select = selector->current(); | ||||||
|  |         if (select.first.isEmpty()) | ||||||
|  |             continue; | ||||||
|  |         m_keys[select.first] = select.second; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // save configuration if required | ||||||
|  |     switch (ret) { | ||||||
|  |     case 0: | ||||||
|  |         break; | ||||||
|  |     case 1: | ||||||
|  |     default: | ||||||
|  |         writeKeys(); | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::initKeys() | ||||||
|  | { | ||||||
|  |     m_keys.clear(); | ||||||
|  |  | ||||||
|  |     QStringList configs = QStandardPaths::locateAll( | ||||||
|  |         QStandardPaths::GenericDataLocation, m_filePath); | ||||||
|  |  | ||||||
|  |     for (auto fileName : configs) { | ||||||
|  |         QSettings settings(fileName, QSettings::IniFormat); | ||||||
|  |         qCInfo(LOG_AW) << "Configuration file" << settings.fileName(); | ||||||
|  |  | ||||||
|  |         settings.beginGroup(QString("Custom")); | ||||||
|  |         QStringList keys = settings.childKeys(); | ||||||
|  |         for (auto key : keys) { | ||||||
|  |             QString source = settings.value(key).toString(); | ||||||
|  |             qCInfo(LOG_AW) << "Found custom key" << key << "for source" | ||||||
|  |                            << source << "in" << settings.fileName(); | ||||||
|  |             if (source.isEmpty()) { | ||||||
|  |                 qCInfo(LOG_AW) << "Skip empty source for" << key; | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |             m_keys[key] = source; | ||||||
|  |         } | ||||||
|  |         settings.endGroup(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void AWCustomKeys::updateDialog() | ||||||
|  | { | ||||||
|  |     clearSelectors(); | ||||||
|  |  | ||||||
|  |     for (auto key : m_keys.keys()) | ||||||
|  |         addSelector(QPair<QString, QString>(key, m_keys[key])); | ||||||
|  |     // empty one | ||||||
|  |     addSelector(QPair<QString, QString>()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | bool AWCustomKeys::writeKeys() const | ||||||
|  | { | ||||||
|  |     QString fileName = QString("%1/%2") | ||||||
|  |                            .arg(QStandardPaths::writableLocation( | ||||||
|  |                                QStandardPaths::GenericDataLocation)) | ||||||
|  |                            .arg(m_filePath); | ||||||
|  |     QSettings settings(fileName, QSettings::IniFormat); | ||||||
|  |     qCInfo(LOG_AW) << "Configuration file" << fileName; | ||||||
|  |  | ||||||
|  |     settings.beginGroup(QString("Custom")); | ||||||
|  |     for (auto key : m_keys.keys()) | ||||||
|  |         settings.setValue(key, m_keys[key]); | ||||||
|  |     settings.endGroup(); | ||||||
|  |  | ||||||
|  |     settings.sync(); | ||||||
|  |  | ||||||
|  |     return (settings.status() == QSettings::NoError); | ||||||
|  | } | ||||||
							
								
								
									
										64
									
								
								sources/awesome-widget/plugin/awcustomkeys.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								sources/awesome-widget/plugin/awcustomkeys.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | |||||||
|  | /*************************************************************************** | ||||||
|  |  *   This file is part of awesome-widgets                                  * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   awesome-widgets is free software: you can redistribute it and/or      * | ||||||
|  |  *   modify it under the terms of the GNU General Public License as        * | ||||||
|  |  *   published by the Free Software Foundation, either version 3 of the    * | ||||||
|  |  *   License, or (at your option) any later version.                       * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   awesome-widgets is distributed in the hope that it will be useful,    * | ||||||
|  |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * | ||||||
|  |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * | ||||||
|  |  *   GNU General Public License for more details.                          * | ||||||
|  |  *                                                                         * | ||||||
|  |  *   You should have received a copy of the GNU General Public License     * | ||||||
|  |  *   along with awesome-widgets. If not, see http://www.gnu.org/licenses/  * | ||||||
|  |  ***************************************************************************/ | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #ifndef AWCUSTOMKEYS_H | ||||||
|  | #define AWCUSTOMKEYS_H | ||||||
|  |  | ||||||
|  | #include <QDialog> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class AWAbstractSelector; | ||||||
|  | namespace Ui | ||||||
|  | { | ||||||
|  | class AWCustomKeys; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | class AWCustomKeys : public QDialog | ||||||
|  | { | ||||||
|  |     Q_OBJECT | ||||||
|  |  | ||||||
|  | public: | ||||||
|  |     explicit AWCustomKeys(QWidget *parent = nullptr, | ||||||
|  |                           const QStringList sources = QStringList()); | ||||||
|  |     virtual ~AWCustomKeys(); | ||||||
|  |     Q_INVOKABLE QStringList customKeys() const; | ||||||
|  |     Q_INVOKABLE void showDialog(); | ||||||
|  |     QVariant valueForKey(const QString &key, const QVariantHash &data); | ||||||
|  |     Q_INVOKABLE QStringList usedSources() const; | ||||||
|  |  | ||||||
|  | private slots: | ||||||
|  |     void updateUi(); | ||||||
|  |  | ||||||
|  | private: | ||||||
|  |     Ui::AWCustomKeys *ui = nullptr; | ||||||
|  |     QList<AWAbstractSelector *> m_selectors; | ||||||
|  |     // properties | ||||||
|  |     QString m_filePath; | ||||||
|  |     QHash<QString, QString> m_keys; | ||||||
|  |     QStringList m_sources; | ||||||
|  |     // methods | ||||||
|  |     void addSelector(const QPair<QString, QString> ¤t); | ||||||
|  |     void clearSelectors(); | ||||||
|  |     void execDialog(); | ||||||
|  |     void initKeys(); | ||||||
|  |     void updateDialog(); | ||||||
|  |     bool writeKeys() const; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif /* AWCUSTOMKEYS_H */ | ||||||
							
								
								
									
										93
									
								
								sources/awesome-widget/plugin/awcustomkeys.ui
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								sources/awesome-widget/plugin/awcustomkeys.ui
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,93 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <ui version="4.0"> | ||||||
|  |  <class>AWCustomKeys</class> | ||||||
|  |  <widget class="QDialog" name="AWCustomKeys"> | ||||||
|  |   <property name="geometry"> | ||||||
|  |    <rect> | ||||||
|  |     <x>0</x> | ||||||
|  |     <y>0</y> | ||||||
|  |     <width>400</width> | ||||||
|  |     <height>300</height> | ||||||
|  |    </rect> | ||||||
|  |   </property> | ||||||
|  |   <layout class="QVBoxLayout" name="verticalLayout_2"> | ||||||
|  |    <item> | ||||||
|  |     <widget class="QScrollArea" name="scrollArea"> | ||||||
|  |      <property name="widgetResizable"> | ||||||
|  |       <bool>true</bool> | ||||||
|  |      </property> | ||||||
|  |      <widget class="QWidget" name="scrollAreaWidgetContents"> | ||||||
|  |       <property name="geometry"> | ||||||
|  |        <rect> | ||||||
|  |         <x>0</x> | ||||||
|  |         <y>0</y> | ||||||
|  |         <width>384</width> | ||||||
|  |         <height>249</height> | ||||||
|  |        </rect> | ||||||
|  |       </property> | ||||||
|  |       <layout class="QVBoxLayout" name="verticalLayout"> | ||||||
|  |        <item> | ||||||
|  |         <spacer name="verticalSpacer"> | ||||||
|  |          <property name="orientation"> | ||||||
|  |           <enum>Qt::Vertical</enum> | ||||||
|  |          </property> | ||||||
|  |          <property name="sizeHint" stdset="0"> | ||||||
|  |           <size> | ||||||
|  |            <width>20</width> | ||||||
|  |            <height>40</height> | ||||||
|  |           </size> | ||||||
|  |          </property> | ||||||
|  |         </spacer> | ||||||
|  |        </item> | ||||||
|  |       </layout> | ||||||
|  |      </widget> | ||||||
|  |     </widget> | ||||||
|  |    </item> | ||||||
|  |    <item> | ||||||
|  |     <widget class="QDialogButtonBox" name="buttonBox"> | ||||||
|  |      <property name="orientation"> | ||||||
|  |       <enum>Qt::Horizontal</enum> | ||||||
|  |      </property> | ||||||
|  |      <property name="standardButtons"> | ||||||
|  |       <set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set> | ||||||
|  |      </property> | ||||||
|  |     </widget> | ||||||
|  |    </item> | ||||||
|  |   </layout> | ||||||
|  |  </widget> | ||||||
|  |  <resources/> | ||||||
|  |  <connections> | ||||||
|  |   <connection> | ||||||
|  |    <sender>buttonBox</sender> | ||||||
|  |    <signal>accepted()</signal> | ||||||
|  |    <receiver>AWCustomKeys</receiver> | ||||||
|  |    <slot>accept()</slot> | ||||||
|  |    <hints> | ||||||
|  |     <hint type="sourcelabel"> | ||||||
|  |      <x>248</x> | ||||||
|  |      <y>254</y> | ||||||
|  |     </hint> | ||||||
|  |     <hint type="destinationlabel"> | ||||||
|  |      <x>157</x> | ||||||
|  |      <y>274</y> | ||||||
|  |     </hint> | ||||||
|  |    </hints> | ||||||
|  |   </connection> | ||||||
|  |   <connection> | ||||||
|  |    <sender>buttonBox</sender> | ||||||
|  |    <signal>rejected()</signal> | ||||||
|  |    <receiver>AWCustomKeys</receiver> | ||||||
|  |    <slot>reject()</slot> | ||||||
|  |    <hints> | ||||||
|  |     <hint type="sourcelabel"> | ||||||
|  |      <x>316</x> | ||||||
|  |      <y>260</y> | ||||||
|  |     </hint> | ||||||
|  |     <hint type="destinationlabel"> | ||||||
|  |      <x>286</x> | ||||||
|  |      <y>274</y> | ||||||
|  |     </hint> | ||||||
|  |    </hints> | ||||||
|  |   </connection> | ||||||
|  |  </connections> | ||||||
|  | </ui> | ||||||
| @ -80,12 +80,14 @@ void AWFormatterConfig::updateUi() | |||||||
|         = m_selectors.indexOf(static_cast<AWAbstractSelector *>(sender())); |         = m_selectors.indexOf(static_cast<AWAbstractSelector *>(sender())); | ||||||
|  |  | ||||||
|     if ((current.first.isEmpty()) && (current.second.isEmpty())) { |     if ((current.first.isEmpty()) && (current.second.isEmpty())) { | ||||||
|  |         // remove current selector if it is empty and does not last | ||||||
|         if (sender() == m_selectors.last()) |         if (sender() == m_selectors.last()) | ||||||
|             return; |             return; | ||||||
|         AWAbstractSelector *selector = m_selectors.takeAt(index); |         AWAbstractSelector *selector = m_selectors.takeAt(index); | ||||||
|         ui->verticalLayout->removeWidget(selector); |         ui->verticalLayout->removeWidget(selector); | ||||||
|         selector->deleteLater(); |         selector->deleteLater(); | ||||||
|     } else { |     } else { | ||||||
|  |         // add new selector if something changed | ||||||
|         if (sender() != m_selectors.last()) |         if (sender() != m_selectors.last()) | ||||||
|             return; |             return; | ||||||
|         auto keys = initKeys(); |         auto keys = initKeys(); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user