5 from PyQt4
.QtCore
import *
6 from PyQt4
.QtGui
import *
10 class ListView(QListView
):
14 options
= self
.viewOptions()
15 for pos
in range(self
.model().rowCount()):
16 index
= self
.model().index(pos
, 0)
17 hint
= self
.itemDelegate().sizeHint(options
, index
)
18 width
= max(width
, hint
.width())
19 height
+= hint
.height()
20 return QSize(width
+ 10, height
+ 10)
23 class Window(QDialog
):
25 QDialog
.__init
__(self
)
26 flags
= self
.windowFlags()
27 self
.setWindowFlags(flags | Qt
.FramelessWindowHint
)
34 lst
= wmctrl
.getWindowList()
35 self
._model
= QStandardItemModel()
37 item
= QStandardItem(unicode(text
, "utf8"))
38 data
= QVariant(QString(wid
))
40 self
._model
.appendRow(item
)
43 def initProxyModel(self
):
44 self
._proxyModel
= QSortFilterProxyModel()
45 self
._proxyModel
.setFilterCaseSensitivity(Qt
.CaseInsensitive
)
46 self
._proxyModel
.setSourceModel(self
._model
)
51 frame
.setFrameStyle(QFrame
.Box | QFrame
.Plain
)
52 layout
= QVBoxLayout(self
)
54 layout
.addWidget(frame
)
57 self
._lineEdit
= QLineEdit(frame
)
58 QObject
.connect(self
._lineEdit
, SIGNAL("textEdited(const QString&)"),
60 QObject
.connect(self
._lineEdit
, SIGNAL("returnPressed()"),
61 self
.slotReturnPressed
)
63 self
._lineEdit
.installEventFilter(self
)
66 self
._view
= ListView(frame
)
67 self
._view
.setModel(self
._proxyModel
)
68 self
._view
.setEditTriggers(QAbstractItemView
.NoEditTriggers
)
69 QObject
.connect(self
._view
, SIGNAL("activated(const QModelIndex&)"),
73 layout
= QVBoxLayout(frame
)
75 layout
.addWidget(self
._lineEdit
)
76 layout
.addWidget(self
._view
)
79 def updateFilter(self
, text
):
80 self
._proxyModel
.setFilterFixedString(text
)
81 if not self
._view
.currentIndex().isValid():
82 firstIndex
= self
._proxyModel
.index(0, 0)
83 if firstIndex
.isValid():
84 self
._view
.setCurrentIndex(firstIndex
)
87 def eventFilter(self
, obj
, event
):
88 if event
.type() != QEvent
.KeyPress
:
91 if event
.key() in (Qt
.Key_Up
, Qt
.Key_Down
):
92 newEvent
= QKeyEvent(event
.type(), event
.key(), event
.modifiers(), event
.text())
93 QApplication
.postEvent(self
._view
, newEvent
)
99 def switchToWindow(self
, index
):
100 sourceIndex
= self
._proxyModel
.mapToSource(index
)
101 item
= self
._model
.itemFromIndex(sourceIndex
)
102 wid
= item
.data().toString()
103 wmctrl
.switchToWindow(unicode(wid
))
107 def slotReturnPressed(self
):
108 index
= self
._view
.currentIndex()
110 self
.switchToWindow(index
)
112 cmd
= unicode(self
._lineEdit
.text())
113 os
.spawnlp(os
.P_NOWAIT
, 'sh', 'sh', '-c', cmd
)
118 app
= QApplication(sys
.argv
)
121 rect
= QApplication
.desktop().availableGeometry()
123 rect
.left() + (rect
.width() - window
.sizeHint().width()) / 2, \
124 rect
.top() + (rect
.height() - window
.sizeHint().height()) / 2 \
130 if __name__
=="__main__":