14 * |
14 * |
15 * You should have received a copy of the GNU General Public License |
15 * You should have received a copy of the GNU General Public License |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 |
18 |
|
19 #include <QPixmap> |
19 #include "model.h" |
20 #include "model.h" |
|
21 |
|
22 static const char* iconPathForElement(const ModelElement& element) |
|
23 { |
|
24 return std::visit(overloaded{ |
|
25 [](const Colored<SubfileReference>&) { |
|
26 return ":/icons/linetype-subfile.png"; |
|
27 }, |
|
28 [](const Colored<LineSegment>&) { |
|
29 return ":/icons/linetype-edgeline.png"; |
|
30 }, |
|
31 [](const Colored<Triangle>&) { |
|
32 return ":/icons/linetype-triangle.png"; |
|
33 }, |
|
34 [](const Colored<Quadrilateral>&) { |
|
35 return ":/icons/linetype-quadrilateral.png"; |
|
36 }, |
|
37 [](const Colored<ConditionalEdge>&) { |
|
38 return ":/icons/linetype-conditionaledge.png"; |
|
39 }, |
|
40 [](const Comment&) { |
|
41 return ":/icons/chatbubble-ellipses-outline.png"; |
|
42 }, |
|
43 [](const Empty&) { |
|
44 return ""; |
|
45 }, |
|
46 [](const ParseError&) { |
|
47 return ":/icons/linetype-errorline.png"; |
|
48 }, |
|
49 }, element); |
|
50 } |
|
51 |
|
52 static QPixmap iconForElement(const ModelElement& element) |
|
53 { |
|
54 // We avoid processing the same image over and over again by storing it |
|
55 // in a static constant. However, we need one per each possible type |
|
56 // of ModelElement, so we put the pixmap constant inside a templated lambda, |
|
57 // which gets instiated once for each type of ModelElement. |
|
58 return std::visit([](auto&& element){ |
|
59 static const QPixmap pixmap = QPixmap::fromImage( |
|
60 QImage{iconPathForElement(element)} |
|
61 .scaledToHeight(24, Qt::SmoothTransformation) |
|
62 ); |
|
63 return pixmap; |
|
64 }, element); |
|
65 } |
20 |
66 |
21 QString modelElementToString(const ModelElement &element) |
67 QString modelElementToString(const ModelElement &element) |
22 { |
68 { |
23 return std::visit(overloaded{ |
69 return std::visit(overloaded{ |
24 [](const Colored<SubfileReference>& ref) { |
70 [](const Colored<SubfileReference>& ref) { |
139 } |
185 } |
140 |
186 |
141 QVariant Model::data(const QModelIndex &index, int role) const |
187 QVariant Model::data(const QModelIndex &index, int role) const |
142 { |
188 { |
143 const int i = index.row(); |
189 const int i = index.row(); |
|
190 const ModelElement& element = this->body[i].data; |
144 switch(role) |
191 switch(role) |
145 { |
192 { |
146 /* |
|
147 case Qt::DecorationRole: |
193 case Qt::DecorationRole: |
148 return QPixmap{object->iconName()}.scaledToHeight(24); |
194 return iconForElement(element); |
149 */ |
|
150 case Qt::DisplayRole: |
195 case Qt::DisplayRole: |
151 return modelElementToString(this->body[i].data); |
196 return modelElementToString(element); |
152 /* |
197 /* |
153 case Qt::ForegroundRole: |
198 case Qt::ForegroundRole: |
154 return object->textRepresentationForeground(); |
199 return object->textRepresentationForeground(); |
155 case Qt::BackgroundRole: |
200 case Qt::BackgroundRole: |
156 return object->textRepresentationBackground(); |
201 return object->textRepresentationBackground(); |