class LegendWidget(Ui_legendsWidget, QWidget): showmap = pyqtSignal() def __init__(self, parent=None): super(LegendWidget, self).__init__(parent) self.setupUi(self) self.gridLayout = QGridLayout(self.legendList) self.gridLayout.setColumnStretch(0,10) self.gridLayout.setColumnStretch(1,300) def show(self,layers): self.dstroyLegend() super(LegendWidget, self).show() self.items = {} for layer in layers: if not layer.type() == QgsMapLayer.VectorLayer: continue try: items = layer.rendererV2().legendSymbologyItems(ICON_SIZE) except AttributeError: continue self.items[layer.name()] = items self.styleLegend(items) def styleLegend(self,items): i=0 for layer, items in self.items.iteritems(): for text, icon in items: if not text or text.startswith("~"): continue self.makeLabel(text,icon,i) i += 1 def makeLabel(self,text,icon,nrow): self.newLabel=QLabel() self.newLabel.setText(text) self.newLabel.setStyleSheet(_fromUtf8("background-color:transparent;")) self.newLabel.setAutoFillBackground(True) self.newIcon=QLabel() self.newIcon.setPixmap(icon) self.newIcon.setStyleSheet(_fromUtf8("background-color:transparent;")) self.newIcon.setAutoFillBackground(True) self.gridLayout.addWidget(self.newIcon,nrow,0,HALLIGNEMENT) self.gridLayout.addWidget(self.newLabel,nrow,1) def makeLabelLayer(self,text,nrow): self.newLabel=QLabel() self.newLabel.setText(text) self.gridLayout.addWidget(self.newLabel,nrow,1) def dstroyLegend(self): while self.gridLayout.count(): self.item = self.gridLayout.itemAt(0).widget() self.gridLayout.removeWidget(self.item) self.item.deleteLater()
class DashboardController(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) self.setWindowFlags(self.windowFlags() | Qt.Tool | Qt.CustomizeWindowHint) self.document = parent self.documentWindow = parent.graphicsView self.layout = QGridLayout() self.layout.setSizeConstraint(QLayout.SetFixedSize) self.layout.setVerticalSpacing(8) self.setLayout(self.layout) self.styleName = str( self.style().metaObject().className())[1:-5].lower() self.qvars = [] def clearInterface(self): while self.layout.count() > 0: widget = self.layout.itemAt(0).widget() self.layout.removeWidget(widget) widget.setParent(None) def numberChanged_(self, value): sender = self.sender() self.document.vars[sender.tag].value = sender.value() / 160. self.document._runScript(compile=False, newSeed=False) def textChanged_(self): sender = self.sender() self.document.vars[sender.tag].value = sender.text() self.document._runScript(compile=False, newSeed=False) def booleanChanged_(self): sender = self.sender() self.document.vars[ sender.tag].value = not self.document.vars[sender.tag].value self.document._runScript(compile=False, newSeed=False) def buttonClicked_(self): sender = self.sender() var = self.document.vars[sender.tag] self.document.fastRun(self.document.namespace[var.name], newSeed=True) def buildInterface_(self, vars): self.vars = vars self.clearInterface() if len(self.vars) > 0: self.show() else: self.hide() self.setWindowTitle(self.document.windowTitle()) cnt = 0 for v in self.vars: if v.type == graphics.NUMBER: self._addLabel(v, cnt) self._addSlider(v, cnt) elif v.type == graphics.TEXT: self._addLabel(v, cnt) self._addTextField(v, cnt) elif v.type == graphics.BOOLEAN: self._addSwitch(v, cnt) elif v.type == graphics.BUTTON: self._addButton(v, cnt) cnt += 1 def _addLabel(self, v, cnt): label = QLabel(v.name + ':') f = QFont(label.font().family()) f.setPointSizeF(11) label.setFont(f) label.setMinimumWidth(80) label.setAlignment(Qt.AlignRight) self.layout.addWidget(label, cnt, 0) def _addSlider(self, v, cnt): control = QSlider(Qt.Horizontal) control.setMinimumWidth(172) control.setFocusPolicy(Qt.StrongFocus) control.setMaximum(v.max * 160) control.setMinimum(v.min * 160) control.setValue(v.value * 160) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacMiniSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("valueChanged(int)"), self.numberChanged_) def _addTextField(self, v, cnt): control = QLineEdit() control.setMinimumWidth(172) control.setMaximumHeight(17) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(9) control.setFont(f) control.setText(v.value) control.tag = cnt self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("returnPressed()"), self.textChanged_) def _addSwitch(self, v, cnt): control = QCheckBox(v.name) control.setChecked(v.value) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(11) control.setFont(f) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacSmallSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("stateChanged(int)"), self.booleanChanged_) def _addButton(self, v, cnt): control = QPushButton(v.name) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(11) if self.styleName == "mac": f.setPointSizeF(10) control.setFont(f) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacMiniSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("clicked()"), self.buttonClicked_)
class TtemView(QAbstractItemView): def __init__(self, parent=None): super(TtemView, self).__init__(parent) self.gridLayout = QGridLayout(self) # # print "*" * 100 # self.listw[:] = [] # print self.listw.__len__() # public: # QRect visualRect(const QModelIndex &index) const # QModelIndex indexAt(const QPoint &point) const # # protected slots: # void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) # void rowsInserted(const QModelIndex &parent, int start, int end) # void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) def dataChanged(self, topLeft, bottomRight): super(TtemView, self).dataChanged(topLeft, bottomRight) print "topLeft : ", topLeft.column(), ",", topLeft.row() print "bottomRight : ", bottomRight.column(), ",", bottomRight.row() print "rowCount:", self.model().rowCount(self.rootIndex()) print "data:", self.model().data(topLeft) row = bottomRight.row() w = self.gridLayout.itemAt(row).widget() w.setText(self.model().data(topLeft)) def edit(self, index, trigger, event): # print "==edit:",index.column(),",",index.row() if not index.isValid(): return False if (index.column() == 0): return super(TtemView, self).edit(index, trigger, event) else: return False def indexAt(self, point): print "=== indexAt ==" return QModelIndex() def isIndexHidden(self, index): print "== isIndexHidden ===" return False def horizontalOffset(self): # print "horizontalOffset" return self.horizontalScrollBar().value() def moveCursor(self, cursorAction, modifiers): current = self.currentIndex() return current def resizeEvent(self, event): # self.updateGeometries() pass def rows(self, index): print "rows" return self.model().rowCount(self.model().parent(index)) def vmWidgetClicked(self): # self.sender() print " vmWidgetClicked, text = ", self.sender().text() ''' 插入行''' def rowsInserted(self, parent, start, end): super(TtemView, self).rowsInserted(parent, start, end) print "rowsInserted" print parent.row(), ":", start, ",", end for i in range(end - start + 1): last = self.gridLayout.count() x = self.gridLayout.count() % 4 # 第x列 y = self.gridLayout.count() / 4 # 第y行 print x, ",", y index = self.model().index(start, 0, self.rootIndex()) text = self.model().data(index) pushBtn = QPushButton(text) self.connect(pushBtn, SIGNAL("clicked()"), self.vmWidgetClicked) self.gridLayout.addWidget(pushBtn, y, x, 1, 1) # row = bottomRight.row() # w =self.gridLayout.itemAt(row).widget() # w.setText(self.model().data(topLeft)) # for (int row = start; row <= end; ++row) # QModelIndex index = model()->index(row, 1, rootIndex()); # double value = model()->data(index).toDouble(); # # if (value > 0.0) # totalValue += value; # validItems++; # QAbstractItemView::rowsInserted(parent, start, end); ''' 删除行''' def rowsAboutToBeRemoved(self, parent, start, end): print "rowsAboutToBeRemoved" super(TtemView, self).rowsAboutToBeRemoved(parent, start, end) # for i in range(end - start + 1): print "---- del ---", start, ",", end if start <= 0 or end < start: return widget_list = self.gridLPopToList() tmp_list = range(start - 1, end) tmp_list.reverse() for i in tmp_list: if i < widget_list.__len__(): print "pop %s" % i w = widget_list.pop(i) w.close() # # if self.gridLayout.count() > 0: # # last = self.gridLayout.count() - 1 # # w = self.gridLayout.takeAt(i).widget() # # w.setParent(None) # # w.close() self.gridLInsertFromeList(widget_list) def gridLPopToList(self): widget_list = [] for i in range(self.gridLayout.count()): last = self.gridLayout.count() - 1 w = self.gridLayout.takeAt(last).widget() w.setParent(None) widget_list.insert(0, w) return widget_list '''从list 中,插入gridlayout''' def gridLInsertFromeList(self, widget_list): for w in widget_list: self.gridLayout.addWidget(w) def verticalOffset(self): return self.verticalScrollBar().value() def visualRect(self, index): # print "visualRect " * 3 # print "visualRect : " * 3 , index.row(),":",index.column() return QRect()
class TtemView(QAbstractItemView): def __init__(self, parent=None): super(TtemView, self).__init__(parent) self.gridLayout = QGridLayout(self) # self.lable = QLabel("testLabel",self) # self.lable.setMinimumSize(100,100) # self.gridLayout.addWidget(self.lable,0,0,1,1) # last = self.gridLayout.count() - 1 # self.gridLayout.addWidget(QPushButton("testLabel1"),0,0,1,1) # self.gridLayout.addWidget(QPushButton("testLabel2"),0,1,1,1) # # self.listw =[] # last = self.gridLayout.count() - 1 # w =self.gridLayout.takeAt(last).widget() # self.listw.append(w) # # # last = self.gridLayout.count() - 1 # # w =self.gridLayout.takeAt(last).widget() # # print type(w) , "#" *10 # # self.listw.append(w) # # print self.listw.__len__() # # self.gridLayout.addWidget(self.listw[0],0,0,1,1) # self.gridLayout.addWidget(self.listw[1],0,1,1,1) # print self.listw.__len__() # # # # print "*" * 100 # self.listw[:] = [] # print self.listw.__len__() # public: # QRect visualRect(const QModelIndex &index) const # void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) # QModelIndex indexAt(const QPoint &point) const # # protected slots: # void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) # void rowsInserted(const QModelIndex &parent, int start, int end) # void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) def dataChanged(self, topLeft, bottomRight): super(TtemView, self).dataChanged(topLeft, bottomRight) print "topLeft : ", topLeft.column(), ",", topLeft.row() print "bottomRight : ", bottomRight.column(), ",", bottomRight.row() print "rowCount:", self.model().rowCount(self.rootIndex()) print "data:", self.model().data(topLeft) w = self.gridLayout.itemAt(0).widget() w.setText(self.model().data(topLeft)) # last = self.gridLayout.count() - 1 # w =self.gridLayout.itemAt(last).widget() # w.setText("1234abc") def edit(self, index, trigger, event): # print "==edit:",index.column(),",",index.row() if not index.isValid(): return False if (index.column() == 0): return super(TtemView, self).edit(index, trigger, event) else: return False def indexAt(self, point): print "=== indexAt ==" return QModelIndex() def isIndexHidden(self, index): print "== isIndexHidden ===" return False def horizontalOffset(self): # print "horizontalOffset" return self.horizontalScrollBar().value() def mousePressEvent(self, event): # print "mousePressEvent" pass # QAbstractItemView::mousePressEvent(event); # origin = event->pos(); # if (!rubberBand) # rubberBand = new QRubberBand(QRubberBand::Rectangle, viewport()); # rubberBand->setGeometry(QRect(origin, QSize())); # rubberBand->show(); def mouseMoveEvent(self, event): pass # if (rubberBand) # rubberBand->setGeometry(QRect(origin, event->pos()).normalized()); # QAbstractItemView::mouseMoveEvent(event); def mouseReleaseEvent(self, event): super(TtemView, self).mouseReleaseEvent(event) # if (rubberBand) # rubberBand->hide(); # viewport()->update(); def moveCursor(self, cursorAction, modifiers): current = self.currentIndex() return current def resizeEvent(self, event): # self.updateGeometries() pass def rows(self, index): print "rows" return self.model().rowCount(self.model().parent(index)) # 插入行 def rowsInserted(self, parent, start, end): super(TtemView, self).rowsInserted(parent, start, end) print "rowsInserted" for i in range(end - start + 1): last = self.gridLayout.count() self.gridLayout.addWidget(QPushButton("...."), 0, last, 1, 1) # for (int row = start; row <= end; ++row) # QModelIndex index = model()->index(row, 1, rootIndex()); # double value = model()->data(index).toDouble(); # # if (value > 0.0) # totalValue += value; # validItems++; # QAbstractItemView::rowsInserted(parent, start, end); # 删除行 def rowsAboutToBeRemoved(self, parent, start, end): print "rowsAboutToBeRemoved" super(TtemView, self).rowsAboutToBeRemoved(parent, start, end) for i in range(end - start + 1): print "---- del ---" if self.gridLayout.count() > 0: last = self.gridLayout.count() - 1 w = self.gridLayout.takeAt(last).widget() w.setParent(None) w.close() # for (int row = start; row <= end; ++row) # # QModelIndex index = model()->index(row, 1, rootIndex()); # double value = model()->data(index).toDouble(); # if (value > 0.0) # totalValue -= value; # validItems--; # # QAbstractItemView::rowsAboutToBeRemoved(parent, start, end); def scrollContentsBy(self, dx, dy): self.viewport().scroll(dx, dy) def scrollTo(self, index, ScrollHint): pass # QRect area = viewport()->rect(); # QRect rect = visualRect(index); # # if (rect.left() < area.left()) # horizontalScrollBar()->setValue( # horizontalScrollBar()->value() + rect.left() - area.left()); # else if (rect.right() > area.right()) # horizontalScrollBar()->setValue( # horizontalScrollBar()->value() + qMin( # rect.right() - area.right(), rect.left() - area.left())); # # if (rect.top() < area.top()) # verticalScrollBar()->setValue( # verticalScrollBar()->value() + rect.top() - area.top()); # else if (rect.bottom() > area.bottom()) # verticalScrollBar()->setValue( # verticalScrollBar()->value() + qMin( # rect.bottom() - area.bottom(), rect.top() - area.top())); # update(); def setSelection(self, rect, command): print "setSelection" # def updateGeometries(self): # pass # # horizontalScrollBar()->setPageStep(viewport()->width()); # # horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width())); # # verticalScrollBar()->setPageStep(viewport()->height()); # # verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height())); def verticalOffset(self): return self.verticalScrollBar().value() def visualRect(self, index): print "visualRect" * 10 print "itemRect : ", index.row() return QRect() # rect = self.itemRect(index) # if (rect.isValid()): # return QRect(rect.left() - self.horizontalScrollBar().value(), # rect.top() - self.verticalScrollBar().value(), # rect.width(), rect.height()) # else: # return rect def visualRegionForSelection(self, selection): print "visualRegionForSelection" ranges = selection.count() if (ranges == 0): return QRect() region = QRegion() # for (int i = 0; i < ranges; ++i) # QItemSelectionRange range = selection.at(i); # for (int row = range.top(); row <= range.bottom(); ++row) # for (int col = range.left(); col <= range.right(); ++col) # QModelIndex index = model()->index(row, col, rootIndex()); # region += visualRect(index); return region
class DashboardController(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) self.setWindowFlags(self.windowFlags() | Qt.Tool | Qt.CustomizeWindowHint) self.document = parent self.documentWindow = parent.graphicsView self.layout = QGridLayout() self.layout.setSizeConstraint(QLayout.SetFixedSize) self.layout.setVerticalSpacing(8) self.setLayout(self.layout) self.styleName = str(self.style().metaObject().className())[1:-5].lower() self.qvars = [] def clearInterface(self): while self.layout.count() > 0: widget = self.layout.itemAt(0).widget() self.layout.removeWidget(widget) widget.setParent(None) def numberChanged_(self, value): sender = self.sender() self.document.vars[sender.tag].value = sender.value() / 160. self.document._runScript(compile=False, newSeed=False) def textChanged_(self): sender = self.sender() self.document.vars[sender.tag].value = sender.text() self.document._runScript(compile=False, newSeed=False) def booleanChanged_(self): sender = self.sender() self.document.vars[sender.tag].value = not self.document.vars[sender.tag].value self.document._runScript(compile=False, newSeed=False) def buttonClicked_(self): sender = self.sender() var = self.document.vars[sender.tag] self.document.fastRun(self.document.namespace[var.name], newSeed=True) def buildInterface_(self, vars): self.vars = vars self.clearInterface() if len(self.vars) > 0: self.show() else: self.hide() self.setWindowTitle(self.document.windowTitle()) cnt = 0 for v in self.vars: if v.type == graphics.NUMBER: self._addLabel(v, cnt) self._addSlider(v, cnt) elif v.type == graphics.TEXT: self._addLabel(v, cnt) self._addTextField(v, cnt) elif v.type == graphics.BOOLEAN: self._addSwitch(v, cnt) elif v.type == graphics.BUTTON: self._addButton(v, cnt) cnt += 1 def _addLabel(self, v, cnt): label = QLabel(v.name + ':') f = QFont(label.font().family()) f.setPointSizeF(11) label.setFont(f) label.setMinimumWidth(80) label.setAlignment(Qt.AlignRight) self.layout.addWidget(label, cnt, 0) def _addSlider(self, v, cnt): control = QSlider(Qt.Horizontal) control.setMinimumWidth(172) control.setFocusPolicy(Qt.StrongFocus) control.setMaximum(v.max * 160) control.setMinimum(v.min * 160) control.setValue(v.value * 160) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacMiniSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("valueChanged(int)"), self.numberChanged_) def _addTextField(self, v, cnt): control = QLineEdit() control.setMinimumWidth(172) control.setMaximumHeight(17) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(9) control.setFont(f) control.setText(v.value) control.tag = cnt self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("returnPressed()"), self.textChanged_) def _addSwitch(self, v, cnt): control = QCheckBox(v.name) control.setChecked(v.value) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(11) control.setFont(f) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacSmallSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("stateChanged(int)"), self.booleanChanged_) def _addButton(self, v, cnt): control = QPushButton(v.name) control.setFocusPolicy(Qt.StrongFocus) f = control.font() f.setPointSizeF(11) if self.styleName == "mac": f.setPointSizeF(10) control.setFont(f) control.tag = cnt if self.styleName == "mac": control.setAttribute(Qt.WA_MacMiniSize, True) self.layout.addWidget(control, cnt, 1) self.connect(control, SIGNAL("clicked()"), self.buttonClicked_)