Mercurial > projects > qtd
comparison demos/browser/modelmenu.d @ 94:87bb4e622f9e
more porting
author | mandel |
---|---|
date | Thu, 28 May 2009 22:11:52 +0000 |
parents | 0654fc9bac95 |
children |
comparison
equal
deleted
inserted
replaced
93:55fd7080c4b9 | 94:87bb4e622f9e |
---|---|
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.QPersistentModelIndex; |
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", string /*text*/); | 56 mixin Signal!("linkHovered", string /*text*/); |
57 | 57 |
58 public: | 58 public: |
59 | 59 |
60 this(QWidget parent = null) | 60 this(QWidget parent = null) |
61 { | 61 { |
158 string 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 = 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 auto v = new QVariant; |
164 v.setValue(parent); | 164 v.setValue(parent); |
165 menu.menuAction().setData(v); | 165 menu.menuAction().setData(v); |
166 connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow())); | 166 menu.aboutToShow.connect(&this.aboutToShow); |
167 return; | 167 return; |
168 } | 168 } |
169 | 169 |
170 int end = m_model.rowCount(parent); | 170 int end = m_model.rowCount(parent); |
171 if (max != -1) | 171 if (max != -1) |
172 end = qMin(max, end); | 172 end = qMin(max, end); |
173 | 173 |
174 menu.triggered.connect(&this.triggered); | 174 menu.triggered.connect(&this.triggered); |
175 menu.hovered.connect(&this.hovered); | 175 menu.linkHovered.connect(&this.linkHovered); |
176 | 176 |
177 for (int i = 0; i < end; ++i) { | 177 for (int i = 0; i < end; ++i) { |
178 QModelIndex idx = m_model.index(i, 0, parent); | 178 QModelIndex idx = m_model.index(i, 0, parent); |
179 if (m_model.hasChildren(idx)) { | 179 if (m_model.hasChildren(idx)) { |
180 createMenu(idx, -1, menu); | 180 createMenu(idx, -1, menu); |
220 QModelIndex idx = cast(QModelIndex) v; | 220 QModelIndex idx = cast(QModelIndex) v; |
221 activated.emit(idx); | 221 activated.emit(idx); |
222 } | 222 } |
223 } | 223 } |
224 | 224 |
225 void hovered(QAction action) | 225 void linkHovered(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 = cast(QModelIndex) v; | 229 QModelIndex idx = cast(QModelIndex) v; |
230 string hoveredString = idx.data(m_hoverRole).toString(); | 230 string hoveredString = idx.data(m_hoverRole).toString(); |
231 if (!hoveredString.isEmpty()) | 231 if (!hoveredString.isEmpty()) |
232 hovered.emit(hoveredString); | 232 linkHovered.emit(hoveredString); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 private: | 236 private: |
237 | 237 |
238 QAction makeAction(QModelIndex index) | 238 QAction makeAction(QModelIndex index) |
239 { | 239 { |
240 QIcon icon = cast(QIcon) index.data(Qt.DecorationRole); | 240 QIcon icon = cast(QIcon) index.data(Qt.DecorationRole); |
241 QAction action = makeAction(icon, index.data().toString(), this); | 241 QAction action = makeAction(icon, index.data().toString(), this); |
242 QVariant v; | 242 auto v = new QVariant; |
243 v.setValue(index); | 243 v.setValue(index); |
244 action.setData(v); | 244 action.setData(v); |
245 return action; | 245 return action; |
246 } | 246 } |
247 | 247 |