| 1 | // |
| 2 | // Copyright (C) 2017 James Turner [email protected] |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU General Public License as |
| 6 | // published by the Free Software Foundation; either version 2 of the |
| 7 | // License, or (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, but |
| 10 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | #include "elementdatamodel.h" |
| 19 | |
| 20 | #include "fgcanvaselement.h" |
| 21 | #include "localprop.h" |
| 22 | |
| 23 | ElementDataModel::ElementDataModel(QObject* pr) |
| 24 | : QAbstractTableModel(pr) |
| 25 | , m_element(nullptr) |
| 26 | { |
| 27 | |
| 28 | } |
| 29 | |
| 30 | void ElementDataModel::setElement(FGCanvasElement *e) |
| 31 | { |
| 32 | beginResetModel(); |
| 33 | m_element = e; |
| 34 | computeKeys(); |
| 35 | endResetModel(); |
| 36 | } |
| 37 | |
| 38 | int ElementDataModel::rowCount(const QModelIndex &parent) const |
| 39 | { |
| 40 | return m_keys.size(); |
| 41 | } |
| 42 | |
| 43 | int ElementDataModel::columnCount(const QModelIndex &parent) const |
| 44 | { |
| 45 | return 2; |
| 46 | } |
| 47 | |
| 48 | QVariant ElementDataModel::data(const QModelIndex &index, int role) const |
| 49 | { |
| 50 | if (!index.isValid() || (index.row() >= m_keys.size())) { |
| 51 | return QVariant(); |
| 52 | } |
| 53 | |
| 54 | QByteArray key = m_keys.at(index.row()); |
| 55 | if (role == Qt::DisplayRole) { |
| 56 | if (index.column() == 0) { |
| 57 | return key; |
| 58 | } |
| 59 | |
| 60 | if (key == "position") { |
| 61 | return m_element->property()->position(); |
| 62 | } |
| 63 | |
| 64 | return m_element->property()->value(key.constData(), QVariant()); |
| 65 | } |
| 66 | |
| 67 | return QVariant(); |
| 68 | } |
| 69 | |
| 70 | void ElementDataModel::computeKeys() |
| 71 | { |
| 72 | m_keys.clear(); |
| 73 | if (m_element == nullptr) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | LocalProp *prop = m_element->property(); |
| 78 | |
| 79 | QByteArrayList directProps = QByteArrayList() << "fill" << "stroke" << |
| 80 | "background" << |
| 81 | "text" << |
| 82 | "clip" << "file" << "src" |
| 83 | "font" << "character-size" << |
| 84 | "z-index" << "visible"; |
| 85 | |
| 86 | Q_FOREACH (QByteArray b, directProps) { |
| 87 | if (prop->hasChild(b)) { |
| 88 | m_keys.append(b); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | m_keys.append("position"); |
| 93 | } |