Mercurial > projects > qtd
comparison demos/browser/modelmenu.d @ 77:0654fc9bac95
more porting
author | mandel |
---|---|
date | Sun, 24 May 2009 13:46:32 +0000 |
parents | 37caa90ce503 |
children | 87bb4e622f9e |
comparison
equal
deleted
inserted
replaced
76:454e4b4beb59 | 77:0654fc9bac95 |
---|---|
39 ** | 39 ** |
40 ****************************************************************************/ | 40 ****************************************************************************/ |
41 module modelmenu; | 41 module modelmenu; |
42 | 42 |
43 | 43 |
44 import qt.gui.QMenu | 44 import qt.gui.QMenu; |
45 import qt.core.QAbstractItemModel; | 45 import qt.core.QAbstractItemModel; |
46 | 46 |
47 import qt.core.QAbstractItemModel; | 47 import qt.core.QAbstractItemModel; |
48 import qdebug; | 48 import qdebug; |
49 | 49 |
50 | 50 |
51 // A QMenu that is dynamically populated from a QAbstractItemModel | 51 // A QMenu that is dynamically populated from a QAbstractItemModel |
52 class ModelMenu : public QMenu | 52 class ModelMenu : public QMenu |
53 { | 53 { |
54 | 54 |
55 mixin Signal!("activated", QModelIndex /*index*/); | 55 mixin Signal!("activated", QModelIndex /*index*/); |
56 mixin Signal!("hovered", QString /*text*/); | 56 mixin Signal!("hovered", string /*text*/); |
57 | 57 |
58 public: | 58 public: |
59 | 59 |
60 this(QWidget parent = null) | 60 this(QWidget parent = null) |
61 { | 61 { |
127 int separatorRole() | 127 int separatorRole() |
128 { | 128 { |
129 return m_separatorRole; | 129 return m_separatorRole; |
130 } | 130 } |
131 | 131 |
132 QAction makeAction(QIcon icon, QString text, QObject parent) | 132 QAction makeAction(QIcon icon, string text, QObject parent) |
133 { | 133 { |
134 auto fm = new QFontMetrics(font()); | 134 auto fm = new QFontMetrics(font()); |
135 if (-1 == m_maxWidth) | 135 if (-1 == m_maxWidth) |
136 m_maxWidth = fm.width(QLatin1Char('m')) * 30; | 136 m_maxWidth = fm.width(QLatin1Char('m')) * 30; |
137 QString smallText = fm.elidedText(text, Qt.ElideMiddle, m_maxWidth); | 137 string smallText = fm.elidedText(text, Qt.ElideMiddle, m_maxWidth); |
138 return new QAction(icon, smallText, parent); | 138 return new QAction(icon, smallText, parent); |
139 } | 139 } |
140 | 140 |
141 protected: | 141 protected: |
142 | 142 |
153 | 153 |
154 // put all of the children of parent into menu up to max | 154 // put all of the children of parent into menu up to max |
155 void createMenu(QModelIndex parent, int max, QMenu parentMenu = null, QMenu menu = null) | 155 void createMenu(QModelIndex parent, int max, QMenu parentMenu = null, QMenu menu = null) |
156 { | 156 { |
157 if (!menu) { | 157 if (!menu) { |
158 QString title = parent.data().toString(); | 158 string title = parent.data().toString(); |
159 menu = new QMenu(title, this); | 159 menu = new QMenu(title, this); |
160 QIcon icon = qvariant_cast<QIcon>(parent.data(Qt.DecorationRole)); | 160 QIcon icon = cast(QIcon) parent.data(Qt.DecorationRole); |
161 menu.setIcon(icon); | 161 menu.setIcon(icon); |
162 parentMenu.addMenu(menu); | 162 parentMenu.addMenu(menu); |
163 QVariant v; | 163 QVariant v; |
164 v.setValue(parent); | 164 v.setValue(parent); |
165 menu.menuAction().setData(v); | 165 menu.menuAction().setData(v); |
191 | 191 |
192 private: | 192 private: |
193 | 193 |
194 void aboutToShow() | 194 void aboutToShow() |
195 { | 195 { |
196 if (QMenu menu = qobject_cast<QMenu>(sender())) { | 196 if (QMenu menu = cast(QMenu) signalSender()) { |
197 QVariant v = menu.menuAction().data(); | 197 QVariant v = menu.menuAction().data(); |
198 if (v.canConvert<QModelIndex>()) { | 198 if (v.canConvert!(QModelIndex)()) { |
199 QModelIndex idx = qvariant_cast<QModelIndex>(v); | 199 QModelIndex idx = cast(QModelIndex) v; |
200 createMenu(idx, -1, menu, menu); | 200 createMenu(idx, -1, menu, menu); |
201 menu.aboutToShow.disconnect(&this.aboutToShow); | 201 menu.aboutToShow.disconnect(&this.aboutToShow); |
202 return; | 202 return; |
203 } | 203 } |
204 } | 204 } |
214 } | 214 } |
215 | 215 |
216 void triggered(QAction action) | 216 void triggered(QAction action) |
217 { | 217 { |
218 QVariant v = action.data(); | 218 QVariant v = action.data(); |
219 if (v.canConvert<QModelIndex>()) { | 219 if (v.canConvert!(QModelIndex)()) { |
220 QModelIndex idx = qvariant_cast<QModelIndex>(v); | 220 QModelIndex idx = cast(QModelIndex) v; |
221 emit activated(idx); | 221 activated.emit(idx); |
222 } | 222 } |
223 } | 223 } |
224 | 224 |
225 void hovered(QAction action) | 225 void hovered(QAction action) |
226 { | 226 { |
227 QVariant v = action.data(); | 227 QVariant v = action.data(); |
228 if (v.canConvert<QModelIndex>()) { | 228 if (v.canConvert!(QModelIndex)()) { |
229 QModelIndex idx = qvariant_cast<QModelIndex>(v); | 229 QModelIndex idx = cast(QModelIndex) v; |
230 QString hoveredString = idx.data(m_hoverRole).toString(); | 230 string hoveredString = idx.data(m_hoverRole).toString(); |
231 if (!hoveredString.isEmpty()) | 231 if (!hoveredString.isEmpty()) |
232 emit hovered(hoveredString); | 232 hovered.emit(hoveredString); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 private: | 236 private: |
237 | 237 |