Warning, file /graphics/tikzkit/src/ui/propertybrowser/OpacityEditorFactory.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "OpacityEditorFactory.h"
0021 #include "OpacityEditor.h"
0022 
0023 namespace tikz {
0024 namespace ui {
0025 
0026 
0027 template <class Editor>
0028 class EditorFactoryPrivate
0029 {
0030 public:
0031 
0032     typedef QList<Editor *> EditorList;
0033     typedef QMap<QtProperty *, EditorList> PropertyToEditorListMap;
0034     typedef QMap<Editor *, QtProperty *> EditorToPropertyMap;
0035 
0036     Editor *createEditor(QtProperty *property, QWidget *parent);
0037     void initializeEditor(QtProperty *property, Editor *e);
0038     void slotEditorDestroyed(QObject *object);
0039 
0040     PropertyToEditorListMap  m_createdEditors;
0041     EditorToPropertyMap m_editorToProperty;
0042 };
0043 
0044 template <class Editor>
0045 Editor *EditorFactoryPrivate<Editor>::createEditor(QtProperty *property, QWidget *parent)
0046 {
0047     Editor *editor = new Editor(parent);
0048     initializeEditor(property, editor);
0049     return editor;
0050 }
0051 
0052 template <class Editor>
0053 void EditorFactoryPrivate<Editor>::initializeEditor(QtProperty *property, Editor *editor)
0054 {
0055     typename PropertyToEditorListMap::iterator it = m_createdEditors.find(property);
0056     if (it == m_createdEditors.end())
0057         it = m_createdEditors.insert(property, EditorList());
0058     it.value().append(editor);
0059     m_editorToProperty.insert(editor, property);
0060 }
0061 
0062 template <class Editor>
0063 void EditorFactoryPrivate<Editor>::slotEditorDestroyed(QObject *object)
0064 {
0065     const typename EditorToPropertyMap::iterator ecend = m_editorToProperty.end();
0066     for (typename EditorToPropertyMap::iterator itEditor = m_editorToProperty.begin(); itEditor !=  ecend; ++itEditor) {
0067         if (itEditor.key() == object) {
0068             Editor *editor = itEditor.key();
0069             QtProperty *property = itEditor.value();
0070             const typename PropertyToEditorListMap::iterator pit = m_createdEditors.find(property);
0071             if (pit != m_createdEditors.end()) {
0072                 pit.value().removeAll(editor);
0073                 if (pit.value().empty())
0074                     m_createdEditors.erase(pit);
0075             }
0076             m_editorToProperty.erase(itEditor);
0077             return;
0078         }
0079     }
0080 }
0081 
0082 class OpacityEditorFactoryPrivate : public EditorFactoryPrivate<OpacityEditor>
0083 {
0084     OpacityEditorFactory *q_ptr;
0085     Q_DECLARE_PUBLIC(OpacityEditorFactory)
0086 public:
0087 
0088     void slotPropertyChanged(QtProperty *property, qreal value);
0089     void slotReadOnlyChanged(QtProperty *property, bool readOnly);
0090     void slotSetValue(qreal value);
0091 };
0092 
0093 void OpacityEditorFactoryPrivate::slotPropertyChanged(QtProperty *property, qreal value)
0094 {
0095     QList<OpacityEditor *> editors = m_createdEditors[property];
0096     QListIterator<OpacityEditor *> itEditor(m_createdEditors[property]);
0097     while (itEditor.hasNext()) {
0098         OpacityEditor *editor = itEditor.next();
0099         if (editor->value() != value) {
0100             editor->blockSignals(true);
0101             editor->setValue(value);
0102             editor->blockSignals(false);
0103         }
0104     }
0105 }
0106 
0107 void OpacityEditorFactoryPrivate::slotReadOnlyChanged(QtProperty *property, bool readOnly)
0108 {
0109     if (!m_createdEditors.contains(property))
0110         return;
0111 
0112     OpacityPropertyManager *manager = q_ptr->propertyManager(property);
0113     if (!manager)
0114         return;
0115 
0116     QListIterator<OpacityEditor *> itEditor(m_createdEditors[property]);
0117     while (itEditor.hasNext()) {
0118         OpacityEditor *editor = itEditor.next();
0119         editor->blockSignals(true);
0120         editor->setReadOnly(readOnly);
0121         editor->blockSignals(false);
0122     }
0123 }
0124 
0125 void OpacityEditorFactoryPrivate::slotSetValue(qreal value)
0126 {
0127     QObject *object = q_ptr->sender();
0128     const QMap<OpacityEditor *, QtProperty *>::ConstIterator itcend = m_editorToProperty.constEnd();
0129     for (QMap<OpacityEditor *, QtProperty *>::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != itcend; ++itEditor) {
0130         if (itEditor.key() == object) {
0131             QtProperty *property = itEditor.value();
0132             OpacityPropertyManager *manager = q_ptr->propertyManager(property);
0133             if (!manager)
0134                 return;
0135             manager->setValue(property, value);
0136             return;
0137         }
0138     }
0139 }
0140 
0141 OpacityEditorFactory::OpacityEditorFactory(QObject *parent)
0142     : QtAbstractEditorFactory<OpacityPropertyManager>(parent)
0143 {
0144     d_ptr = new OpacityEditorFactoryPrivate();
0145     d_ptr->q_ptr = this;
0146 
0147 }
0148 
0149 OpacityEditorFactory::~OpacityEditorFactory()
0150 {
0151     qDeleteAll(d_ptr->m_editorToProperty.keys());
0152     delete d_ptr;
0153 }
0154 
0155 void OpacityEditorFactory::connectPropertyManager(OpacityPropertyManager *manager)
0156 {
0157     connect(manager, SIGNAL(valueChanged(QtProperty *, qreal)),
0158                 this, SLOT(slotPropertyChanged(QtProperty *, qreal)));
0159     connect(manager, SIGNAL(readOnlyChanged(QtProperty *, bool)),
0160                 this, SLOT(slotReadOnlyChanged(QtProperty *, bool)));
0161 }
0162 
0163 QWidget *OpacityEditorFactory::createEditor(OpacityPropertyManager *manager,
0164         QtProperty *property, QWidget *parent)
0165 {
0166     OpacityEditor *editor = d_ptr->createEditor(property, parent);
0167     editor->setValue(manager->value(property));
0168     editor->setReadOnly(manager->isReadOnly(property));
0169 
0170     connect(editor, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetValue(qreal)));
0171     connect(editor, SIGNAL(destroyed(QObject *)),
0172                 this, SLOT(slotEditorDestroyed(QObject *)));
0173     return editor;
0174 }
0175 
0176 void OpacityEditorFactory::disconnectPropertyManager(OpacityPropertyManager *manager)
0177 {
0178     disconnect(manager, SIGNAL(valueChanged(QtProperty *, qreal)),
0179                 this, SLOT(slotPropertyChanged(QtProperty *, qreal)));
0180     disconnect(manager, SIGNAL(readOnlyChanged(QtProperty *, bool)),
0181                 this, SLOT(slotReadOnlyChanged(QtProperty *, bool)));
0182 }
0183 
0184 }
0185 }
0186 
0187 #include "moc_OpacityEditorFactory.cpp"
0188 
0189 // kate: indent-width 4; replace-tabs on;