def __init__(self, pixmap, title="", parent=None): QGraphicsWidget.__init__(self, parent) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100)
def __init__(self, pixmap, title="", parentItem=None, **kwargs): super().__init__(parentItem, **kwargs) self.setFocusPolicy(Qt.StrongFocus) self._title = None self._size = QSizeF() layout = QGraphicsLinearLayout(Qt.Vertical, self) layout.setSpacing(2) layout.setContentsMargins(5, 5, 5, 5) self.setContentsMargins(0, 0, 0, 0) self.pixmapWidget = GraphicsPixmapWidget(pixmap, self) self.labelWidget = GraphicsTextWidget(title, self) layout.addItem(self.pixmapWidget) layout.addItem(self.labelWidget) layout.addStretch() layout.setAlignment(self.pixmapWidget, Qt.AlignCenter) layout.setAlignment(self.labelWidget, Qt.AlignHCenter | Qt.AlignBottom) self.setLayout(layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setTitle(title) self.setTitleWidth(100)
class TurnOffScreen(plasmascript.Applet): def __init__(self, parent, args = None): plasmascript.Applet.__init__(self, parent) def init(self): #TODO: have a configuration interface to set keybroad shortcut #self.setHasConfigurationInterface(True) self.setAspectRatioMode(Plasma.ConstrainedSquare) self.setBackgroundHints(self.NoBackground) self.sessionBus = dbus.SessionBus() self.powerdevil = self.sessionBus.get_object('org.freedesktop.PowerManagement', '/modules/powerdevil') self.icon= Plasma.IconWidget(KIcon('preferences-desktop-screensaver'), '', self.applet) if KGlobalSettings.singleClick(): self.connect(self.icon, SIGNAL('clicked()'), self.turn_off_screen) else: self.connect(self.icon, SIGNAL('doubleClicked()'), self.turn_off_screen) self.connect(self, SIGNAL('active()'), self.turn_off_screen) self.layout = QGraphicsLinearLayout(self.applet) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(0) self.layout.addItem(self.icon) self.setLayout(self.layout) self.resize(25, 25) #def showConfigurationInterface(self): # self.con_short = KShortcutWidget(None) # self.con_short.show() def turn_off_screen(self): self.powerdevil.turnOffScreen(dbus_interface='org.kde.PowerDevil')
def __init__(self, labels=[], orientation=Qt.Vertical, alignment=Qt.AlignCenter, parent=None): QGraphicsWidget.__init__(self, parent) layout = QGraphicsLinearLayout(orientation) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.setLayout(layout) self.orientation = orientation self.alignment = alignment self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.label_items = [] self.set_labels(labels)
def __init__(self, labels=[], orientation=Qt.Vertical, parent=None): QGraphicsWidget.__init__(self, parent) layout = QGraphicsLinearLayout(orientation) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.setLayout(layout) self.orientation = orientation self.alignment = Qt.AlignCenter self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.label_items = [] self.set_labels(labels)
class TurnOffScreen(plasmascript.Applet): def __init__(self, parent, args=None): plasmascript.Applet.__init__(self, parent) def init(self): #TODO: have a configuration interface to set keybroad shortcut #self.setHasConfigurationInterface(True) self.setAspectRatioMode(Plasma.ConstrainedSquare) self.setBackgroundHints(self.NoBackground) self.sessionBus = dbus.SessionBus() self.powerdevil = self.sessionBus.get_object( 'org.freedesktop.PowerManagement', '/modules/powerdevil') self.icon = Plasma.IconWidget(KIcon('preferences-desktop-screensaver'), '', self.applet) if KGlobalSettings.singleClick(): self.connect(self.icon, SIGNAL('clicked()'), self.turn_off_screen) else: self.connect(self.icon, SIGNAL('doubleClicked()'), self.turn_off_screen) self.connect(self, SIGNAL('active()'), self.turn_off_screen) self.layout = QGraphicsLinearLayout(self.applet) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(0) self.layout.addItem(self.icon) self.setLayout(self.layout) self.resize(25, 25) #def showConfigurationInterface(self): # self.con_short = KShortcutWidget(None) # self.con_short.show() def turn_off_screen(self): self.powerdevil.turnOffScreen(dbus_interface='org.kde.PowerDevil')
class OWHierarchicalClustering(widget.OWWidget): name = "Hierarchical Clustering" description = ("Hierarchical clustering based on distance matrix, and " "a dendrogram viewer.") icon = "icons/HierarchicalClustering.svg" priority = 2100 inputs = [("Distances", Orange.misc.DistMatrix, "set_distances")] outputs = [("Selected Data", Orange.data.Table), ("Other Data", Orange.data.Table)] #: Selected linkage linkage = settings.Setting(1) #: Index of the selected annotation item (variable, ...) annotation_idx = settings.Setting(0) #: Selected tree pruning (none/max depth) pruning = settings.Setting(0) #: Maximum depth when max depth pruning is selected max_depth = settings.Setting(10) #: Selected cluster selection method (none, cut distance, top n) selection_method = settings.Setting(0) #: Cut height ratio wrt root height cut_ratio = settings.Setting(75.0) #: Number of top clusters to select top_n = settings.Setting(3) append_clusters = settings.Setting(True) cluster_role = settings.Setting(2) cluster_name = settings.Setting("Cluster") autocommit = settings.Setting(False) #: Cluster variable domain role AttributeRole, ClassRole, MetaRole = 0, 1, 2 def __init__(self, parent=None): super().__init__(parent) self.matrix = None self.items = None self.linkmatrix = None self.root = None self._displayed_root = None self.cutoff_height = 0.0 self._invalidated = False gui.comboBox(gui.widgetBox(self.controlArea, "Linkage"), self, "linkage", items=LINKAGE, callback=self._invalidate_clustering) box = gui.widgetBox(self.controlArea, "Annotation") self.label_cb = gui.comboBox(box, self, "annotation_idx", callback=self._update_labels) self.label_cb.setModel(itemmodels.VariableListModel()) self.label_cb.model()[:] = ["None", "Enumeration"] box = gui.radioButtons(self.controlArea, self, "pruning", box="Pruning", callback=self._invalidate_pruning) grid = QGridLayout() box.layout().addLayout(grid) grid.addWidget(gui.appendRadioButton(box, "None", addToLayout=False), 0, 0) self.max_depth_spin = gui.spin(box, self, "max_depth", minv=1, maxv=100, callback=self._invalidate_pruning, keyboardTracking=False) grid.addWidget( gui.appendRadioButton(box, "Max depth", addToLayout=False), 1, 0) grid.addWidget(self.max_depth_spin, 1, 1) box = gui.radioButtons(self.controlArea, self, "selection_method", box="Selection", callback=self._selection_method_changed) grid = QGridLayout() box.layout().addLayout(grid) grid.addWidget(gui.appendRadioButton(box, "Manual", addToLayout=False), 0, 0) grid.addWidget( gui.appendRadioButton(box, "Height ratio", addToLayout=False), 1, 0) self.cut_ratio_spin = gui.spin(box, self, "cut_ratio", 0, 100, step=1e-1, spinType=float, callback=self._selection_method_changed) self.cut_ratio_spin.setSuffix("%") grid.addWidget(self.cut_ratio_spin, 1, 1) grid.addWidget(gui.appendRadioButton(box, "Top N", addToLayout=False), 2, 0) self.top_n_spin = gui.spin(box, self, "top_n", 1, 20, callback=self._selection_method_changed) grid.addWidget(self.top_n_spin, 2, 1) box.layout().addLayout(grid) self.controlArea.layout().addStretch() box = gui.widgetBox(self.controlArea, "Output") gui.checkBox(box, self, "append_clusters", "Append cluster IDs", callback=self._invalidate_output) ibox = gui.indentedBox(box) name_edit = gui.lineEdit(ibox, self, "cluster_name") name_edit.editingFinished.connect(self._invalidate_output) cb = gui.comboBox( ibox, self, "cluster_role", callback=self._invalidate_output, items=["Attribute", "Class variable", "Meta variable"]) form = QFormLayout(fieldGrowthPolicy=QFormLayout.AllNonFixedFieldsGrow, labelAlignment=Qt.AlignLeft, spacing=8) form.addRow("Name", name_edit) form.addRow("Place", cb) ibox.layout().addSpacing(5) ibox.layout().addLayout(form) ibox.layout().addSpacing(5) cb = gui.checkBox(box, self, "autocommit", "Commit automatically") b = gui.button(box, self, "Commit", callback=self.commit, default=True) gui.setStopper(self, b, cb, "_invalidated", callback=self.commit) self.scene = QGraphicsScene() self.view = QGraphicsView( self.scene, horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOff, verticalScrollBarPolicy=Qt.ScrollBarAlwaysOn, alignment=Qt.AlignLeft | Qt.AlignVCenter) def axis_view(orientation): ax = pg.AxisItem(orientation=orientation, maxTickLength=7) scene = QGraphicsScene() scene.addItem(ax) view = QGraphicsView( scene, horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOff, verticalScrollBarPolicy=Qt.ScrollBarAlwaysOn, alignment=Qt.AlignLeft | Qt.AlignVCenter) view.setFixedHeight(ax.size().height()) ax.line = SliderLine(orientation=Qt.Horizontal, length=ax.size().height()) scene.addItem(ax.line) return view, ax self.top_axis_view, self.top_axis = axis_view("top") self.mainArea.layout().setSpacing(1) self.mainArea.layout().addWidget(self.top_axis_view) self.mainArea.layout().addWidget(self.view) self.bottom_axis_view, self.bottom_axis = axis_view("bottom") self.mainArea.layout().addWidget(self.bottom_axis_view) self._main_graphics = QGraphicsWidget() self._main_layout = QGraphicsLinearLayout(Qt.Horizontal) self._main_layout.setSpacing(1) self._main_graphics.setLayout(self._main_layout) self.scene.addItem(self._main_graphics) self.dendrogram = DendrogramWidget() self.dendrogram.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.dendrogram.selectionChanged.connect(self._invalidate_output) self.dendrogram.selectionEdited.connect(self._selection_edited) fm = self.fontMetrics() self.dendrogram.setContentsMargins(5, fm.lineSpacing() / 2, 5, fm.lineSpacing() / 2) self.labels = GraphicsSimpleTextList() self.labels.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.labels.setAlignment(Qt.AlignLeft) self.labels.setMaximumWidth(200) self.labels.layout().setSpacing(0) self._main_layout.addItem(self.dendrogram) self._main_layout.addItem(self.labels) self._main_layout.setAlignment(self.dendrogram, Qt.AlignLeft | Qt.AlignVCenter) self._main_layout.setAlignment(self.labels, Qt.AlignLeft | Qt.AlignVCenter) self.view.viewport().installEventFilter(self) self.top_axis_view.viewport().installEventFilter(self) self.bottom_axis_view.viewport().installEventFilter(self) self._main_graphics.installEventFilter(self) self.cut_line = SliderLine(self.dendrogram, orientation=Qt.Horizontal) self.cut_line.valueChanged.connect(self._dendrogram_slider_changed) self.cut_line.hide() self.bottom_axis.line.valueChanged.connect(self._axis_slider_changed) self.top_axis.line.valueChanged.connect(self._axis_slider_changed) self.dendrogram.geometryChanged.connect(self._dendrogram_geom_changed) self._set_cut_line_visible(self.selection_method == 1) def set_distances(self, matrix): self.matrix = matrix self._invalidate_clustering() self._set_items(matrix.row_items if matrix is not None else None) def _set_items(self, items): self.items = items if items is None: self.label_cb.model()[:] = ["None", "Enumeration"] elif isinstance(items, Orange.data.Table): vars = list(items.domain) self.label_cb.model()[:] = ["None", "Enumeration"] + vars elif isinstance(items, list) and \ all(isinstance(var, Orange.data.Variable) for var in items): self.label_cb.model()[:] = ["None", "Enumeration", "Name"] else: self.label_cb.model()[:] = ["None", "Enumeration"] self.annotation_idx = min(self.annotation_idx, len(self.label_cb.model()) - 1) def handleNewSignals(self): self._update_labels() def _clear_plot(self): self.labels.set_labels([]) self.dendrogram.set_root(None) def _set_displayed_root(self, root): self._clear_plot() self._displayed_root = root self.dendrogram.set_root(root) self._update_labels() self._main_graphics.resize( self._main_graphics.size().width(), self._main_graphics.sizeHint(Qt.PreferredSize).height()) self._main_graphics.layout().activate() def _update(self): self._clear_plot() distances = self.matrix if distances is not None: # Convert to flat upper triangular distances i, j = numpy.triu_indices(distances.X.shape[0], k=1) distances = distances.X[i, j] method = LINKAGE[self.linkage].lower() Z = scipy.cluster.hierarchy.linkage(distances, method=method) tree = tree_from_linkage(Z) self.linkmatrix = Z self.root = tree self.top_axis.setRange(tree.value.height, 0.0) self.bottom_axis.setRange(tree.value.height, 0.0) if self.pruning: self._set_displayed_root(prune(tree, level=self.max_depth)) else: self._set_displayed_root(tree) else: self.linkmatrix = None self.root = None self._set_displayed_root(None) self._apply_selection() def _update_labels(self): labels = [] if self.root and self._displayed_root: indices = [leaf.value.index for leaf in leaves(self.root)] if self.annotation_idx == 0: labels = [] elif self.annotation_idx == 1: labels = [str(i) for i in indices] elif isinstance(self.items, Orange.data.Table): var = self.label_cb.model()[self.annotation_idx] col = self.items[:, var] labels = [var.repr_val(next(iter(row))) for row in col] labels = [labels[idx] for idx in indices] else: labels = [] if labels and self._displayed_root is not self.root: joined = leaves(self._displayed_root) labels = [ ", ".join(labels[leaf.value.first:leaf.value.last]) for leaf in joined ] self.labels.set_labels(labels) self.labels.setMinimumWidth(1 if labels else -1) def _invalidate_clustering(self): self._update() self._update_labels() def _invalidate_output(self): self._invalidated = True if self.autocommit: self.commit() def _invalidate_pruning(self): if self.root: selection = self.dendrogram.selected_nodes() ranges = [node.value.range for node in selection] if self.pruning: self._set_displayed_root(prune(self.root, level=self.max_depth)) else: self._set_displayed_root(self.root) selected = [ node for node in preorder(self._displayed_root) if node.value.range in ranges ] self.dendrogram.set_selected_clusters(selected) self._apply_selection() def commit(self): self._invalidated = False items = getattr(self.matrix, "items", self.items) if not items: # nothing to commit return selection = self.dendrogram.selected_nodes() selection = sorted(selection, key=lambda c: c.value.first) indices = [leaf.value.index for leaf in leaves(self.root)] maps = [ indices[node.value.first:node.value.last] for node in selection ] selected_indices = list(chain(*maps)) unselected_indices = sorted( set(range(self.root.value.last)) - set(selected_indices)) selected = [items[k] for k in selected_indices] unselected = [items[k] for k in unselected_indices] if not selected: self.send("Selected Data", None) self.send("Other Data", None) return selected_data = unselected_data = None if isinstance(items, Orange.data.Table): c = numpy.zeros(len(items)) for i, indices in enumerate(maps): c[indices] = i c[unselected_indices] = len(maps) mask = c != len(maps) if self.append_clusters: clust_var = Orange.data.DiscreteVariable( str(self.cluster_name), values=[ "Cluster {}".format(i + 1) for i in range(len(maps)) ] + ["Other"]) data, domain = items, items.domain attrs = domain.attributes class_ = domain.class_vars metas = domain.metas X, Y, M = data.X, data.Y, data.metas if self.cluster_role == self.AttributeRole: attrs = attrs + (clust_var, ) X = numpy.c_[X, c] elif self.cluster_role == self.ClassRole: class_ = class_ + (clust_var, ) Y = numpy.c_[Y, c] elif self.cluster_role == self.MetaRole: metas = metas + (clust_var, ) M = numpy.c_[M, c] domain = Orange.data.Domain(attrs, class_, metas) data = Orange.data.Table(domain, X, Y, M) else: data = items if selected: selected_data = data[mask] if unselected: unselected_data = data[~mask] self.send("Selected Data", selected_data) self.send("Other Data", unselected_data) def sizeHint(self): return QSize(800, 500) def eventFilter(self, obj, event): if obj is self.view.viewport() and event.type() == QEvent.Resize: width = self.view.viewport().width() - 2 self._main_graphics.setMaximumWidth(width) self._main_graphics.setMinimumWidth(width) self._main_graphics.layout().activate() elif event.type() == QEvent.MouseButtonPress and \ (obj is self.top_axis_view.viewport() or obj is self.bottom_axis_view.viewport()): self.selection_method = 1 # Map click point to cut line local coordinates pos = self.top_axis_view.mapToScene(event.pos()) cut = self.top_axis.line.mapFromScene(pos) self.top_axis.line.setValue(cut.x()) # update the line visibility, output, ... self._selection_method_changed() return super().eventFilter(obj, event) def _dendrogram_geom_changed(self): pos = self.dendrogram.pos_at_height(self.cutoff_height) geom = self.dendrogram.geometry() crect = self.dendrogram.contentsRect() self._set_slider_value(pos.x(), geom.width()) self.cut_line.setLength(geom.height()) self.top_axis.resize(crect.width(), self.top_axis.height()) self.top_axis.setPos(geom.left() + crect.left(), 0) self.top_axis.line.setPos(self.cut_line.scenePos().x(), 0) self.bottom_axis.resize(crect.width(), self.bottom_axis.height()) self.bottom_axis.setPos(geom.left() + crect.left(), 0) self.bottom_axis.line.setPos(self.cut_line.scenePos().x(), 0) geom = self._main_graphics.geometry() assert geom.topLeft() == QPointF(0, 0) self.scene.setSceneRect(geom) geom.setHeight(self.top_axis.size().height()) self.top_axis.scene().setSceneRect(geom) self.bottom_axis.scene().setSceneRect(geom) def _axis_slider_changed(self, value): self.cut_line.setValue(value) def _dendrogram_slider_changed(self, value): p = QPointF(value, 0) cl_height = self.dendrogram.height_at(p) self.set_cutoff_height(cl_height) # Sync the cut positions between the dendrogram and the axis. self._set_slider_value(value, self.dendrogram.size().width()) def _set_slider_value(self, value, span): with blocked(self.cut_line): self.cut_line.setValue(value) self.cut_line.setRange(0, span) with blocked(self.top_axis.line): self.top_axis.line.setValue(value) self.top_axis.line.setRange(0, span) with blocked(self.bottom_axis.line): self.bottom_axis.line.setValue(value) self.bottom_axis.line.setRange(0, span) def set_cutoff_height(self, height): self.cutoff_height = height if self.root: self.cut_ratio = 100 * height / self.root.value.height self.select_max_height(height) def _set_cut_line_visible(self, visible): self.cut_line.setVisible(visible) self.top_axis.line.setVisible(visible) self.bottom_axis.line.setVisible(visible) def select_top_n(self, n): root = self._displayed_root if root: clusters = top_clusters(root, n) self.dendrogram.set_selected_clusters(clusters) def select_max_height(self, height): root = self._displayed_root if root: clusters = clusters_at_height(root, height) self.dendrogram.set_selected_clusters(clusters) def _selection_method_changed(self): self._set_cut_line_visible(self.selection_method == 1) if self.root: self._apply_selection() def _apply_selection(self): if not self.root: return if self.selection_method == 0: pass elif self.selection_method == 1: height = self.cut_ratio * self.root.value.height / 100 self.set_cutoff_height(height) pos = self.dendrogram.pos_at_height(height) self._set_slider_value(pos.x(), self.dendrogram.size().width()) elif self.selection_method == 2: self.select_top_n(self.top_n) def _selection_edited(self): # Selection was edited by clicking on a cluster in the # dendrogram view. self.selection_method = 0 self._selection_method_changed()
class OWHierarchicalClustering(widget.OWWidget): name = "Hierarchical Clustering" description = ("Hierarchical clustering based on distance matrix, and " "a dendrogram viewer.") icon = "icons/HierarchicalClustering.svg" priority = 2100 inputs = [("Distances", Orange.misc.DistMatrix, "set_distances")] outputs = [("Selected Data", Orange.data.Table), ("Other Data", Orange.data.Table)] #: Selected linkage linkage = settings.Setting(1) #: Index of the selected annotation item (variable, ...) annotation_idx = settings.Setting(0) #: Selected tree pruning (none/max depth) pruning = settings.Setting(0) #: Maximum depth when max depth pruning is selected max_depth = settings.Setting(10) #: Selected cluster selection method (none, cut distance, top n) selection_method = settings.Setting(0) #: Cut height ratio wrt root height cut_ratio = settings.Setting(75.0) #: Number of top clusters to select top_n = settings.Setting(3) append_clusters = settings.Setting(True) cluster_role = settings.Setting(2) cluster_name = settings.Setting("Cluster") autocommit = settings.Setting(False) #: Cluster variable domain role AttributeRole, ClassRole, MetaRole = 0, 1, 2 def __init__(self, parent=None): super().__init__(parent) self.matrix = None self.items = None self.linkmatrix = None self.root = None self._displayed_root = None self.cutoff_height = 0.0 gui.comboBox(gui.widgetBox(self.controlArea, "Linkage"), self, "linkage", items=LINKAGE, callback=self._invalidate_clustering) box = gui.widgetBox(self.controlArea, "Annotation") self.label_cb = gui.comboBox( box, self, "annotation_idx", callback=self._update_labels) self.label_cb.setModel(itemmodels.VariableListModel()) self.label_cb.model()[:] = ["None", "Enumeration"] box = gui.radioButtons( self.controlArea, self, "pruning", box="Pruning", callback=self._invalidate_pruning ) grid = QGridLayout() box.layout().addLayout(grid) grid.addWidget( gui.appendRadioButton(box, "None", addToLayout=False), 0, 0 ) self.max_depth_spin = gui.spin( box, self, "max_depth", minv=1, maxv=100, callback=self._invalidate_pruning, keyboardTracking=False ) grid.addWidget( gui.appendRadioButton(box, "Max depth", addToLayout=False), 1, 0) grid.addWidget(self.max_depth_spin, 1, 1) box = gui.radioButtons( self.controlArea, self, "selection_method", box="Selection", callback=self._selection_method_changed) grid = QGridLayout() box.layout().addLayout(grid) grid.addWidget( gui.appendRadioButton(box, "Manual", addToLayout=False), 0, 0 ) grid.addWidget( gui.appendRadioButton(box, "Height ratio", addToLayout=False), 1, 0 ) self.cut_ratio_spin = gui.spin( box, self, "cut_ratio", 0, 100, step=1e-1, spinType=float, callback=self._selection_method_changed ) self.cut_ratio_spin.setSuffix("%") grid.addWidget(self.cut_ratio_spin, 1, 1) grid.addWidget( gui.appendRadioButton(box, "Top N", addToLayout=False), 2, 0 ) self.top_n_spin = gui.spin(box, self, "top_n", 1, 20, callback=self._selection_method_changed) grid.addWidget(self.top_n_spin, 2, 1) box.layout().addLayout(grid) self.controlArea.layout().addStretch() box = gui.widgetBox(self.controlArea, "Output") gui.checkBox(box, self, "append_clusters", "Append cluster IDs", callback=self._invalidate_output) ibox = gui.indentedBox(box) name_edit = gui.lineEdit(ibox, self, "cluster_name") name_edit.editingFinished.connect(self._invalidate_output) cb = gui.comboBox( ibox, self, "cluster_role", callback=self._invalidate_output, items=["Attribute", "Class variable", "Meta variable"] ) form = QFormLayout( fieldGrowthPolicy=QFormLayout.AllNonFixedFieldsGrow, labelAlignment=Qt.AlignLeft, spacing=8 ) form.addRow("Name", name_edit) form.addRow("Place", cb) ibox.layout().addSpacing(5) ibox.layout().addLayout(form) ibox.layout().addSpacing(5) gui.auto_commit(box, self, "autocommit", "Send data", "Auto send is on", box=False) self.scene = QGraphicsScene() self.view = QGraphicsView( self.scene, horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOff, verticalScrollBarPolicy=Qt.ScrollBarAlwaysOn, alignment=Qt.AlignLeft | Qt.AlignVCenter ) def axis_view(orientation): ax = pg.AxisItem(orientation=orientation, maxTickLength=7) scene = QGraphicsScene() scene.addItem(ax) view = QGraphicsView( scene, horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOff, verticalScrollBarPolicy=Qt.ScrollBarAlwaysOn, alignment=Qt.AlignLeft | Qt.AlignVCenter ) view.setFixedHeight(ax.size().height()) ax.line = SliderLine(orientation=Qt.Horizontal, length=ax.size().height()) scene.addItem(ax.line) return view, ax self.top_axis_view, self.top_axis = axis_view("top") self.mainArea.layout().setSpacing(1) self.mainArea.layout().addWidget(self.top_axis_view) self.mainArea.layout().addWidget(self.view) self.bottom_axis_view, self.bottom_axis = axis_view("bottom") self.mainArea.layout().addWidget(self.bottom_axis_view) self._main_graphics = QGraphicsWidget() self._main_layout = QGraphicsLinearLayout(Qt.Horizontal) self._main_layout.setSpacing(1) self._main_graphics.setLayout(self._main_layout) self.scene.addItem(self._main_graphics) self.dendrogram = DendrogramWidget() self.dendrogram.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.dendrogram.selectionChanged.connect(self._invalidate_output) self.dendrogram.selectionEdited.connect(self._selection_edited) fm = self.fontMetrics() self.dendrogram.setContentsMargins( 5, fm.lineSpacing() / 2, 5, fm.lineSpacing() / 2 ) self.labels = GraphicsSimpleTextList() self.labels.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.labels.setAlignment(Qt.AlignLeft) self.labels.setMaximumWidth(200) self.labels.layout().setSpacing(0) self._main_layout.addItem(self.dendrogram) self._main_layout.addItem(self.labels) self._main_layout.setAlignment( self.dendrogram, Qt.AlignLeft | Qt.AlignVCenter) self._main_layout.setAlignment( self.labels, Qt.AlignLeft | Qt.AlignVCenter) self.view.viewport().installEventFilter(self) self.top_axis_view.viewport().installEventFilter(self) self.bottom_axis_view.viewport().installEventFilter(self) self._main_graphics.installEventFilter(self) self.cut_line = SliderLine(self.dendrogram, orientation=Qt.Horizontal) self.cut_line.valueChanged.connect(self._dendrogram_slider_changed) self.cut_line.hide() self.bottom_axis.line.valueChanged.connect(self._axis_slider_changed) self.top_axis.line.valueChanged.connect(self._axis_slider_changed) self.dendrogram.geometryChanged.connect(self._dendrogram_geom_changed) self._set_cut_line_visible(self.selection_method == 1) def set_distances(self, matrix): self.matrix = matrix self._invalidate_clustering() self._set_items(matrix.row_items if matrix is not None else None) def _set_items(self, items): self.items = items if items is None: self.label_cb.model()[:] = ["None", "Enumeration"] elif isinstance(items, Orange.data.Table): vars = list(items.domain) self.label_cb.model()[:] = ["None", "Enumeration"] + vars elif isinstance(items, list) and \ all(isinstance(var, Orange.data.Variable) for var in items): self.label_cb.model()[:] = ["None", "Enumeration", "Name"] else: self.label_cb.model()[:] = ["None", "Enumeration"] self.annotation_idx = min(self.annotation_idx, len(self.label_cb.model()) - 1) def handleNewSignals(self): self._update_labels() def _clear_plot(self): self.labels.set_labels([]) self.dendrogram.set_root(None) def _set_displayed_root(self, root): self._clear_plot() self._displayed_root = root self.dendrogram.set_root(root) self._update_labels() self._main_graphics.resize( self._main_graphics.size().width(), self._main_graphics.sizeHint(Qt.PreferredSize).height() ) self._main_graphics.layout().activate() def _update(self): self._clear_plot() distances = self.matrix if distances is not None: # Convert to flat upper triangular distances i, j = numpy.triu_indices(distances.X.shape[0], k=1) distances = distances.X[i, j] method = LINKAGE[self.linkage].lower() Z = scipy.cluster.hierarchy.linkage( distances, method=method ) tree = tree_from_linkage(Z) self.linkmatrix = Z self.root = tree self.top_axis.setRange(tree.value.height, 0.0) self.bottom_axis.setRange(tree.value.height, 0.0) if self.pruning: self._set_displayed_root(prune(tree, level=self.max_depth)) else: self._set_displayed_root(tree) else: self.linkmatrix = None self.root = None self._set_displayed_root(None) self._apply_selection() def _update_labels(self): labels = [] if self.root and self._displayed_root: indices = [leaf.value.index for leaf in leaves(self.root)] if self.annotation_idx == 0: labels = [] elif self.annotation_idx == 1: labels = [str(i) for i in indices] elif isinstance(self.items, Orange.data.Table): var = self.label_cb.model()[self.annotation_idx] col = self.items[:, var] labels = [var.repr_val(next(iter(row))) for row in col] labels = [labels[idx] for idx in indices] else: labels = [] if labels and self._displayed_root is not self.root: joined = leaves(self._displayed_root) labels = [", ".join(labels[leaf.value.first: leaf.value.last]) for leaf in joined] self.labels.set_labels(labels) self.labels.setMinimumWidth(1 if labels else -1) def _invalidate_clustering(self): self._update() self._update_labels() def _invalidate_output(self): self.commit() def _invalidate_pruning(self): if self.root: selection = self.dendrogram.selected_nodes() ranges = [node.value.range for node in selection] if self.pruning: self._set_displayed_root( prune(self.root, level=self.max_depth)) else: self._set_displayed_root(self.root) selected = [node for node in preorder(self._displayed_root) if node.value.range in ranges] self.dendrogram.set_selected_clusters(selected) self._apply_selection() def commit(self): items = getattr(self.matrix, "items", self.items) if not items: # nothing to commit return selection = self.dendrogram.selected_nodes() selection = sorted(selection, key=lambda c: c.value.first) indices = [leaf.value.index for leaf in leaves(self.root)] maps = [indices[node.value.first:node.value.last] for node in selection] selected_indices = list(chain(*maps)) unselected_indices = sorted(set(range(self.root.value.last)) - set(selected_indices)) selected = [items[k] for k in selected_indices] unselected = [items[k] for k in unselected_indices] if not selected: self.send("Selected Data", None) self.send("Other Data", None) return selected_data = unselected_data = None if isinstance(items, Orange.data.Table): c = numpy.zeros(len(items)) for i, indices in enumerate(maps): c[indices] = i c[unselected_indices] = len(maps) mask = c != len(maps) if self.append_clusters: clust_var = Orange.data.DiscreteVariable( str(self.cluster_name), values=["Cluster {}".format(i + 1) for i in range(len(maps))] + ["Other"], ordered=True ) data, domain = items, items.domain attrs = domain.attributes class_ = domain.class_vars metas = domain.metas if self.cluster_role == self.AttributeRole: attrs = attrs + (clust_var,) elif self.cluster_role == self.ClassRole: class_ = class_ + (clust_var,) elif self.cluster_role == self.MetaRole: metas = metas + (clust_var,) domain = Orange.data.Domain(attrs, class_, metas) data = Orange.data.Table(domain, data) data.get_column_view(clust_var)[0][:] = c else: data = items if selected: selected_data = data[mask] if unselected: unselected_data = data[~mask] self.send("Selected Data", selected_data) self.send("Other Data", unselected_data) def sizeHint(self): return QSize(800, 500) def eventFilter(self, obj, event): if obj is self.view.viewport() and event.type() == QEvent.Resize: width = self.view.viewport().width() - 2 self._main_graphics.setMaximumWidth(width) self._main_graphics.setMinimumWidth(width) self._main_graphics.layout().activate() elif event.type() == QEvent.MouseButtonPress and \ (obj is self.top_axis_view.viewport() or obj is self.bottom_axis_view.viewport()): self.selection_method = 1 # Map click point to cut line local coordinates pos = self.top_axis_view.mapToScene(event.pos()) cut = self.top_axis.line.mapFromScene(pos) self.top_axis.line.setValue(cut.x()) # update the line visibility, output, ... self._selection_method_changed() return super().eventFilter(obj, event) def _dendrogram_geom_changed(self): pos = self.dendrogram.pos_at_height(self.cutoff_height) geom = self.dendrogram.geometry() crect = self.dendrogram.contentsRect() self._set_slider_value(pos.x(), geom.width()) self.cut_line.setLength(geom.height()) self.top_axis.resize(crect.width(), self.top_axis.height()) self.top_axis.setPos(geom.left() + crect.left(), 0) self.top_axis.line.setPos(self.cut_line.scenePos().x(), 0) self.bottom_axis.resize(crect.width(), self.bottom_axis.height()) self.bottom_axis.setPos(geom.left() + crect.left(), 0) self.bottom_axis.line.setPos(self.cut_line.scenePos().x(), 0) geom = self._main_graphics.geometry() assert geom.topLeft() == QPointF(0, 0) self.scene.setSceneRect(geom) geom.setHeight(self.top_axis.size().height()) self.top_axis.scene().setSceneRect(geom) self.bottom_axis.scene().setSceneRect(geom) def _axis_slider_changed(self, value): self.cut_line.setValue(value) def _dendrogram_slider_changed(self, value): p = QPointF(value, 0) cl_height = self.dendrogram.height_at(p) self.set_cutoff_height(cl_height) # Sync the cut positions between the dendrogram and the axis. self._set_slider_value(value, self.dendrogram.size().width()) def _set_slider_value(self, value, span): with blocked(self.cut_line): self.cut_line.setValue(value) self.cut_line.setRange(0, span) with blocked(self.top_axis.line): self.top_axis.line.setValue(value) self.top_axis.line.setRange(0, span) with blocked(self.bottom_axis.line): self.bottom_axis.line.setValue(value) self.bottom_axis.line.setRange(0, span) def set_cutoff_height(self, height): self.cutoff_height = height if self.root: self.cut_ratio = 100 * height / self.root.value.height self.select_max_height(height) def _set_cut_line_visible(self, visible): self.cut_line.setVisible(visible) self.top_axis.line.setVisible(visible) self.bottom_axis.line.setVisible(visible) def select_top_n(self, n): root = self._displayed_root if root: clusters = top_clusters(root, n) self.dendrogram.set_selected_clusters(clusters) def select_max_height(self, height): root = self._displayed_root if root: clusters = clusters_at_height(root, height) self.dendrogram.set_selected_clusters(clusters) def _selection_method_changed(self): self._set_cut_line_visible(self.selection_method == 1) if self.root: self._apply_selection() def _apply_selection(self): if not self.root: return if self.selection_method == 0: pass elif self.selection_method == 1: height = self.cut_ratio * self.root.value.height / 100 self.set_cutoff_height(height) pos = self.dendrogram.pos_at_height(height) self._set_slider_value(pos.x(), self.dendrogram.size().width()) elif self.selection_method == 2: self.select_top_n(self.top_n) def _selection_edited(self): # Selection was edited by clicking on a cluster in the # dendrogram view. self.selection_method = 0 self._selection_method_changed()
class UBlogApplet(plasmascript.Applet): def __init__(self, parent, **kwargs): plasmascript.Applet.__init__(self, parent) self._layout = None self.flash = None self.tab_bar = None self.ui = None self.status_edit = None self.scroll_widget = None self.tweets_layout = None self.main_frame = None self.pm = None self.consumer = oauth.Consumer(CONSUMER_KEY, CONSUMER_SECRET) self.client = oauth.Client(self.consumer) self.icon = None self.oauth_secret = None self.oauth_key = None self.history_size = 10 self.timer = QTimer(self) self.history_refresh = 1 self.tweets_widget = None self.message_id = None self._wallet_timer = QTimer(self) def init(self): """ create interface, this method invoked by plasma it self """ self.setHasConfigurationInterface(True) self.setAspectRatioMode(Plasma.IgnoreAspectRatio) self.setBackgroundHints(Plasma.Applet.DefaultBackground) # self.flash = Plasma.FlashingLabel(self.applet) self.flash.setAutohide(True) self.flash.setMinimumSize(0, 20) self.flash.setDuration(2000) self.tab_bar = Plasma.TabBar() self._layout = QGraphicsLinearLayout(Qt.Vertical, self.applet) self._layout.setSpacing(3) flash_layout = QGraphicsLinearLayout(Qt.Horizontal) flash_layout.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) fnt = Plasma.Theme.defaultTheme().font(Plasma.Theme.DefaultFont) fnt.setBold(True) fm = QFontMetrics(fnt) self.flash.setFont(fnt) self.flash.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) title_layout = QGraphicsLinearLayout(Qt.Vertical) flash_layout.addItem(self.flash) flash_layout.addItem(title_layout) self.main_frame = Plasma.Frame(self.applet) m_header_layout = QGraphicsAnchorLayout(self.main_frame) m_header_layout.setSpacing(5) self.icon = Plasma.IconWidget(self.main_frame) self.icon.setIcon(KIcon("user-identity")) self.icon.setTextBackgroundColor(QColor()) icon_size = self.icon.sizeFromIconSize(48) self.icon.setMinimumSize(icon_size) self.icon.setMaximumSize(icon_size) m_header_layout.addAnchor(self.icon, Qt.AnchorVerticalCenter, m_header_layout, Qt.AnchorVerticalCenter) m_header_layout.addAnchor(self.icon, Qt.AnchorLeft, m_header_layout, Qt.AnchorLeft) status_edit_frame = Plasma.Frame(self.main_frame) status_edit_frame.setFrameShadow(Plasma.Frame.Sunken) status_edit_layout = QGraphicsLinearLayout(status_edit_frame) self.status_edit = Plasma.TextEdit() self.status_edit.setPreferredHeight(fm.height() * 4) self.status_edit.setEnabled(False) status_edit_layout.addItem(self.status_edit) edit_pal = self.status_edit.palette() m_color_scheme = KColorScheme(QPalette.Active, KColorScheme.View, Plasma.Theme.defaultTheme().colorScheme()) edit_pal.setColor(QPalette.Text, m_color_scheme.foreground().color()) self.status_edit.nativeWidget().setPalette(edit_pal) self.status_edit.nativeWidget().installEventFilter(self) m_header_layout.addAnchor(self.icon, Qt.AnchorRight, status_edit_frame, Qt.AnchorLeft) m_header_layout.addAnchors(status_edit_frame, m_header_layout, Qt.Vertical) m_header_layout.addAnchor(status_edit_frame, Qt.AnchorRight, m_header_layout, Qt.AnchorRight) m_header_layout.activate() m_header_layout.setMaximumHeight(m_header_layout.effectiveSizeHint(Qt.PreferredSize).height()) self.scroll_widget = Plasma.ScrollWidget(self.applet) self.tweets_widget = QGraphicsWidget(self.scroll_widget) self.scroll_widget.setWidget(self.tweets_widget) self.tweets_layout = QGraphicsLinearLayout(Qt.Vertical, self.tweets_widget) self.tweets_layout.setSpacing(3) self.tweets_layout.addItem(self.main_frame) self.tab_bar.addTab(self.trUtf8("Timeline")) self.tab_bar.addTab(self.trUtf8("Replies")) self.tab_bar.addTab(self.trUtf8("Messages")) self._layout.addItem(flash_layout) self._layout.addItem(self.tab_bar) self._layout.addItem(self.scroll_widget) self.applet.setLayout(self._layout) self.connect(self.tab_bar, SIGNAL('currentChanged(int)'), self.mode_changed) self.connect(self.status_edit, SIGNAL('textChanged()'), self.edit_text_changed) self.check_config() def check_config(self): if self.pm is None: self._wallet_timer.setSingleShot(True) self._wallet_timer.setInterval(1000) self.connect(self._wallet_timer, SIGNAL("timeout()"), self.open_wallet); self._wallet_timer.start() return None self.oauth_secret = unicode(self.pm.readPassword("twitter_secret")[1]) self.oauth_key = unicode(self.pm.readPassword("twitter_token")[1]) self.history_size = int(self.pm.readEntry("historySize")[1]) self.history_refresh = int(self.pm.readEntry("historyRefresh")[1]) if self.history_size == '': self.history_size = 10 if self.history_refresh is None or self.history_refresh == '': self.history_refresh = 5 if self.oauth_key == '' or self.oauth_secret == '': self.authenticate() else: self.status_edit.setEnabled(True) self.connect(self.timer, SIGNAL('timeout()'), self.update) self.update() self.timer.start(self.history_refresh * 60 * 1000) def open_wallet(self): if self.view() is None: self._wallet_timer.start() return None if self.view().winId() is None: self._wallet_timer.start() return None self.pm = PasswordManager(self.view().winId()) self.check_config() def edit_text_changed(self): remaining_char = 140 - self.status_edit.nativeWidget().toPlainText().length() self.flash.flash(unicode(self.trUtf8("%s character left", "%s character left")) % remaining_char, 2000) def createConfigurationInterface(self, dialog): """ create configuration settings for user parameters """ self.connect(dialog, SIGNAL('applyClicked()'), self.config_accepted) self.connect(dialog, SIGNAL('okClicked()'), self.config_accepted) widget = QWidget(dialog) self.ui = uic.loadUi(self.package().filePath('ui', 'configuration.ui'), widget) history_size = self.pm.readEntry("historySize")[1] history_refresh = self.pm.readEntry("historyRefresh")[1] if history_size: self.ui.historySizeSpinBox.setValue(int(str(history_size))) if history_refresh: self.ui.historyRefreshSpinBox.setValue(int(str(history_refresh))) dialog.addPage(widget, self.trUtf8("General"), "view-pim-journal") def config_accepted(self): """ we must update timer object after these settings changed """ self.pm.writeEntry("historyRefresh", str(self.ui.historyRefreshSpinBox.value())) self.pm.writeEntry("historySize", str(self.ui.historySizeSpinBox.value())) self.history_size = str(self.ui.historyRefreshSpinBox.value()) self.history_refresh = int(self.ui.historySizeSpinBox.value()) self.status_edit.setEnabled(True) self.timer.stop() self.timer.start(self.history_refresh * 60 * 1000) self.update() def mode_changed(self): self.flash.flash(self.trUtf8("Refreshing timeline...")) self.timer.stop() self.update() self.timer.start(int(self.history_refresh) * 60 * 1000) def update(self): self.flash.flash(self.trUtf8("Refreshing timeline...")) current_idx = self.tab_bar.currentIndex() if current_idx == 0: self.__update_timeline() elif current_idx == 1: self.__update_replies() else: self.__update_messages() def __make_rest_calls(self, url, user=True): token = oauth.Token(self.oauth_key, self.oauth_secret) client = oauth.Client(self.consumer, token=token) resp, content = client.request(url+"?count="+str(self.history_size)) self.tweets_widget.prepareGeometryChange() if resp['status'] == '200': # we must clear all tweets widgets before for i in xrange(0, self.tweets_layout.count()-1): widget = self.tweets_layout.itemAt(1) if isinstance(widget, TweetWidget): widget.deleteLater() self.tweets_layout.removeAt(1) tweets = json.loads(content) for tweet in tweets: widget = TweetWidget(self.tweets_widget) widget.set_data(tweet, user=user) self.connect(widget, SIGNAL('reply(QString, QString)'), self.reply) self.connect(widget, SIGNAL('profile(QString)'), self.profile) self.connect(widget, SIGNAL('retweet(QString)'), self.retweet) self.connect(widget, SIGNAL('favorite(QString, bool)'), self.favorite) self.tweets_layout.addItem(widget) self.layout() def __update_timeline(self): self.__make_rest_calls("https://api.twitter.com/1.1/statuses/home_timeline.json") def __update_messages(self): self.__make_rest_calls("https://api.twitter.com/1.1/direct_messages.json", user=False) def __update_replies(self): self.__make_rest_calls("https://api.twitter.com/1.1/statuses/mentions_timeline.json") def reply(self, message_id, authorname): self.status_edit.setText(authorname + ' ') self.message_id = message_id def profile(self, user): KToolInvocation.invokeBrowser("https://twitter.com/%s" % (user,)) def __make_post_calls(self, url, body): self.timer.stop() token = oauth.Token(self.oauth_key, self.oauth_secret) client = oauth.Client(self.consumer, token=token) resp, content = client.request(url, method='POST', body=body) if resp['status'] == '200': self.update() self.timer.start() def retweet(self, message_id): self.flash.flash(self.trUtf8("Retweetting...")) self.__make_post_calls("https://api.twitter.com/1.1/statuses/retweet/"+str(message_id)+".json", body='id='+str(message_id)) def favorite(self, message_id, add): if add: self.flash.flash(self.trUtf8("Adding favorites...")) else: self.flash.flash(self.trUtf8("Removing from favorites...")) self.__make_post_calls("https://api.twitter.com/1.1/favorites/"+("create" if add else "destroy")+".json", body='id='+str(message_id)) def update_status(self): tweet = unicode(self.status_edit.nativeWidget().toPlainText()) self.status_edit.setText(' ') self.flash.flash(self.trUtf8("Tweet sending...")) self.setBusy(True) body = 'status='+tweet if tweet.startswith('@') and self.message_id is not None: body += '&in_reply_to_status_id='+str(self.message_id) self.message_id = None self.__make_post_calls("https://api.twitter.com/1.1/statuses/update.json", body=body) self.setBusy(False) def eventFilter(self, obj, event): if isinstance(obj, KTextEdit): if event.type() == QEvent.KeyPress: key_event = QKeyEvent(event) key = key_event.key() if (key_event.modifiers() == Qt.ControlModifier) and (key == Qt.Key_Enter or key == Qt.Key_Return): self.update_status() return True safe_keys = [Qt.Key_Delete, Qt.Key_Backspace, Qt.Key_Up, Qt.Key_Down, Qt.Key_Right, Qt.Key_Left, Qt.Key_Home, Qt.Key_End] if key not in safe_keys: if self.status_edit.nativeWidget().toPlainText().length() >= 140: return True return False elif isinstance(obj, KTabBar) and event.type() == QEvent.MouseButtonPress: self.scroll_widget.ensureItemVisible(self.main_frame) self.status_edit.setFocus() return False else: return self.applet.eventFilter(obj, event) def authenticate(self, loop_count): if loop_count >= 5: return self.quit() loop_count += 1 resp, content = self.client.request("https://twitter.com/oauth/request_token", "GET") if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) request_token = dict(urlparse.parse_qsl(content)) KToolInvocation.invokeBrowser("https://twitter.com/oauth/authorize?oauth_token=%s&oauth_callback=oob" % ( request_token['oauth_token'])) dialog = KInputDialog.getText(self.trUtf8("PIN"), self.trUtf8("Enter the PIN received from Twitter:")) if dialog[1] is True and not dialog[0].isEmpty(): token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(str(dialog[0])) client = oauth.Client(self.consumer, token) resp, content = client.request("https://twitter.com/oauth/access_token", "POST") if resp['status'] == '200': access_token = dict(urlparse.parse_qsl(content)) self.oauth_secret = access_token['oauth_token_secret'] self.oauth_key = access_token['oauth_token'] self.pm.writePassword("twitter_secret", self.oauth_secret) self.pm.writePassword("twitter_token", self.oauth_key) else: self.authenticate(loop_count) else: self.quit() def quit(self): self.close()