class Karmack4(plasmascript.Applet): def __init__(self, parent, args=None): plasmascript.Applet.__init__(self, parent) def init(self): self.applet.resize(480.0, 300.0) qmlView = Plasma.DeclarativeWidget() qmlView.setQmlPath("karmack4/contents/ui/main.qml") self.layout = QGraphicsLinearLayout(self.applet) self.layout.setOrientation(Qt.Vertical) self.layout.addItem(qmlView) helloLabel = Plasma.Label(self.applet) helloLabel.setText(PYQT_VERSION_STR) self.layout.addItem(helloLabel) buttonek = Plasma.PushButton(self.applet) buttonek.setText("Klikni a uvidis!") self.layout.addItem(buttonek) self.qmlRootObject = qmlView.rootObject() self.connect(buttonek, SIGNAL("clicked()"), self.buttonClicked) self.connect(KWindowSystem.self(), SIGNAL("windowAdded(WId)"), self.onWokynkoNove) self.connect(KWindowSystem.self(), SIGNAL("windowRemoved(WId)"), self.onWokynkoFuc) def buttonClicked(self): #print dir(self.qmlRootObject) self.qmlRootObject.updateQml() def onWokynkoNove(self): self.qmlRootObject.windowAdded() def onWokynkoFuc(self): self.qmlRootObject.windowRemoved()
class PylouWidget(QGraphicsWidget): """Main Widget for Pylou.""" def __init__(self, parent): """Init class.""" QGraphicsWidget.__init__(self) self.applet = parent def init(self): """Start Pylou Widget.""" self.layou = QGraphicsLinearLayout(self) self.stringlist = QStringList() self.model = QStringListModel(self.applet) self.model.setStringList(self.stringlist) self.treeview = MyTreeView(self) self.treeview.setModel(self.model) self.lineEdit, self.label = MyLineEdit(self), Plasma.Label(self) self.label.setText("Search") self.layou.setOrientation(0x2) # Qt.Vertical self.layou.addItem(self.treeview) self.layou.addItem(self.label) self.layou.addItem(self.lineEdit) self.setLayout(self.layou) self.lineEdit.returnPressed.connect(self.addItem) self.setMinimumSize(200, 99) self.setMaximumSize(666, 666) # custom user choosed fonts user_font_family = QVariant(self.applet.configurations.readEntry( "TextFont", QVariant(QFont()))) self.treeview.nativeWidget().setFont(QFont(user_font_family)) # custom user choosed styles user_style_sheet = "color:{};alternate-background-color:{}".format( self.applet.configurations.readEntry("TextColor"), self.applet.configurations.readEntry("AlternateBColor")) self.treeview.nativeWidget().setStyleSheet(user_style_sheet) # Qt connecting people Applet.connect( self.lineEdit, SIGNAL("keyUPPressed"), self.prevHistoryItem) Applet.connect( self.lineEdit, SIGNAL("keyDownPressed"), self.nextHistoryItem) Applet.connect(self.treeview, SIGNAL("DblClick"), self.openFile) Applet.connect(self.treeview, SIGNAL("Click"), self.openDirectory) self.applet.appletDestroyed.connect(self.saveHistory) # History file self.histfile = HISTORY_FILE_PATH with open(self.histfile, 'r') as history_file: self.history = history_file.readlines() self.historyCurrentItem = 0 self.treeview.nativeWidget().hide() self.resize(self.minimumSize()) def saveHistory(self): """Write History to History file.""" with open(self.histfile, 'w') as history_file: history_file.writelines(self.history) def prevHistoryItem(self): """Navigate the History 1 Item Backwards.""" if self.historyCurrentItem < len(self.history): self.historyCurrentItem = self.historyCurrentItem + 1 try: self.lineEdit.setText(str(self.history[-self.historyCurrentItem])) except IndexError as error: print(error) self.label.setText("ERROR: History Empty.") def nextHistoryItem(self): """Navigate the History 1 Item Forwards.""" if self.historyCurrentItem > 1: self.historyCurrentItem = self.historyCurrentItem - 1 try: self.lineEdit.setText(str(self.history[-self.historyCurrentItem])) except IndexError as error: print(error) self.label.setText("ERROR: History Empty.") def addItem(self): """Add Items from Locate command.""" start_time = datetime.now().second self.stringlist.clear() lineText = self.lineEdit.text() if len(lineText) and str(lineText).strip() not in self.history: self.history.append(lineText + "\n") self.historyCurrentItem = 1 self.saveHistory() self.historyCurrentItem = self.historyCurrentItem - 1 command = "ionice --ignore --class 3 chrt --idle 0 " # Nice CPU / IO command += "locate --ignore-case --existing --quiet --limit 9999 {}" condition = str(self.applet.configurations.readEntry("Home")) == "true" if len(str(lineText).strip()) and condition: command_to_run = command.format( # Only Search inside Home folders path.join(path.expanduser("~"), "*{}*".format(lineText))) else: command_to_run = command.format(lineText) locate_output = Popen(command_to_run, shell=True, stdout=PIPE).stdout results = tuple(locate_output.readlines()) banned = self.applet.configurations.readEntry("Banned") banned_regex_pattern = str(banned).strip().lower().replace(" ", "|") for item in results: if not search(banned_regex_pattern, str(item)): # banned words self.stringlist.append(item[:-1]) purge() # Purge RegEX Cache self.model.setStringList(self.stringlist) self.treeview.nativeWidget().resizeColumnToContents(0) number_of_results = len(results) if number_of_results: # if tems found Focus on item list self.lineEdit.nativeWidget().clear() self.label.setText("Found {} results on {} seconds !".format( number_of_results, abs(datetime.now().second - start_time))) self.resize(500, 12 * number_of_results) self.treeview.nativeWidget().show() self.treeview.nativeWidget().setFocus() else: # if no items found Focus on LineEdit self.label.setText("Search") self.resize(self.minimumSize()) self.treeview.nativeWidget().hide() self.lineEdit.nativeWidget().selectAll() self.lineEdit.nativeWidget().setFocus() def openDirectory(self, index): """Take a model index and find the folder name then open the folder.""" item_to_open = path.dirname(str(self.model.data(index, 0).toString())) Popen("xdg-open '{}'".format(item_to_open), shell=True) def openFile(self, index): """Take a model index and find the filename then open the file.""" item_to_open = self.model.data(index, 0).toString() Popen("xdg-open '{}'".format(item_to_open), shell=True)