SF.net SVN: fclient: [47] trunk/fclient/fclient_lib
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2007-11-05 12:50:37
|
Revision: 47
http://fclient.svn.sourceforge.net/fclient/?rev=47&view=rev
Author: jUrner
Date: 2007-11-05 04:50:42 -0800 (Mon, 05 Nov 2007)
Log Message:
-----------
svn fix
Added Paths:
-----------
trunk/fclient/fclient_lib/qt4ex/
trunk/fclient/fclient_lib/qt4ex/LICENCE.MIT
trunk/fclient/fclient_lib/qt4ex/README
trunk/fclient/fclient_lib/qt4ex/__init__.py
trunk/fclient/fclient_lib/qt4ex/assistant.py
trunk/fclient/fclient_lib/qt4ex/ctrls/
trunk/fclient/fclient_lib/qt4ex/ctrls/__init__.py
trunk/fclient/fclient_lib/qt4ex/ctrls/areatips.py
trunk/fclient/fclient_lib/qt4ex/ctrls/checkarraywrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/colorbutton.py
trunk/fclient/fclient_lib/qt4ex/ctrls/compactpatwrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/dragtool.py
trunk/fclient/fclient_lib/qt4ex/ctrls/editboxwrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/labelwrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/mrumenu.py
trunk/fclient/fclient_lib/qt4ex/ctrls/progressbarwrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/tablewidget.py
trunk/fclient/fclient_lib/qt4ex/ctrls/toolbarwrap.py
trunk/fclient/fclient_lib/qt4ex/ctrls/treewidgetwrap.py
trunk/fclient/fclient_lib/qt4ex/dlgs/
trunk/fclient/fclient_lib/qt4ex/dlgs/__init__.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgabout/
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgabout/DlgAbout.ui
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgabout/Ui_DlgAbout.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgabout/__init__.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgfindreplace/
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgfindreplace/DlgFindReplace.ui
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgfindreplace/Ui_DlgFindReplace.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgfindreplace/__init__.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgpreferences/
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgpreferences/DlgPreferencesTree.ui
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgpreferences/Ui_DlgPreferencesTree.py
trunk/fclient/fclient_lib/qt4ex/dlgs/dlgpreferences/__init__.py
trunk/fclient/fclient_lib/qt4ex/lang/
trunk/fclient/fclient_lib/qt4ex/lang/qt4ex_de.ts
trunk/fclient/fclient_lib/qt4ex/lang/qt4ex_en.ts
trunk/fclient/fclient_lib/qt4ex/language.py
trunk/fclient/fclient_lib/qt4ex/qt4ex.pro
trunk/fclient/fclient_lib/qt4ex/qtools.py
trunk/fclient/fclient_lib/qt4ex/res/
trunk/fclient/fclient_lib/qt4ex/res/language/
trunk/fclient/fclient_lib/qt4ex/res/language/LangCodes-ISO 639-1.txt
trunk/fclient/fclient_lib/qt4ex/resources.py
trunk/fclient/fclient_lib/qt4ex/scripts/
trunk/fclient/fclient_lib/qt4ex/scripts/__init__.py
trunk/fclient/fclient_lib/qt4ex/scripts/manifest.py
trunk/fclient/fclient_lib/qt4ex/scripts/pylupdate.py
trunk/fclient/fclient_lib/qt4ex/scripts/qtpro.py
trunk/fclient/fclient_lib/qt4ex/settingsbase.py
Added: trunk/fclient/fclient_lib/qt4ex/LICENCE.MIT
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/LICENCE.MIT (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/LICENCE.MIT 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,21 @@
+
+qt4ex -- Qt5 / PyQt4 extensions
+
+Copyright (c) 2007 J\xFCrgen Urner
+
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial
+portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
+OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
Added: trunk/fclient/fclient_lib/qt4ex/README
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/README (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/README 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,23 @@
+ qt4ex -- Qt5 / PyQt4 extensions
+
+
+
+Version history:
+
+*******************************************************************
+0.1.0
+*******************************************************************
+news:
+ x. initial release
+
+bugfixes:
+ x.
+
+
+
+
+
+
+
+
+
Added: trunk/fclient/fclient_lib/qt4ex/__init__.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/__init__.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/__init__.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,11 @@
+"""Qt4 extensions
+
+"""
+
+__version__ = '0.1.0'
+__author__ = 'Juergen Urner'
+__emeil__ = 'jU...@ar...'
+__licence__ = 'Mit'
+__copyright__ = '(c) 2007 Juergen Urner'
+
+
Added: trunk/fclient/fclient_lib/qt4ex/assistant.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/assistant.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/assistant.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,130 @@
+
+import os
+from PyQt4 import QtCore, QtGui
+
+__version__ = '0.0.1'
+#*****************************************************************
+#
+#*****************************************************************
+class Assistant(QtCore.QObject):
+ """Class to tame QAssistant a bit
+
+ Usage:
+
+ * Feed a profile and a dict of pages to the class
+ * call showPage() with the widget and the name of a page to show as params
+ * call close() with the widget as param the page is shown for when the widget is closed
+
+ An internal refcount is kept to enshure QAssistant is not closed any sooner
+ than as when the last widget refering to it is closed. And as a bonus closing
+ a page for a widget will switch back to the page opend by the previous widget
+
+ @note: currently only one open page / widget is supported
+ """
+ def __init__(self, parent, profile, pages):
+ """
+ @param profile: (absolute) path of the profile file to dfeed to assistant
+ @param pages: a dict{name-of-page: fpath-page} of pages
+ """
+ QtCore.QObject.__init__(self, parent)
+
+ self._referers = [[], []] # [widgets, page-names]
+ self._isInited = False
+
+ self.qtAssistant = None
+ self.pages = pages
+ self.parent = parent
+ self.profile = profile
+
+ if self.pages is None:
+ self.pages = {}
+
+
+ ################################################
+ ## slots
+ ################################################
+ def onAssistantClosed(self):
+ self._referers = [[], []]
+
+ ################################################
+ ## methods
+ ################################################
+ def close(self, parent):
+ """Closes assistant if open and no other widget currently has a page open
+ """
+ if self.qtAssistant is not None:
+ widgets, pages = self._referers
+ if parent in widgets:
+ i = widgets.index(parent)
+ del widgets[i]
+ del pages[i]
+ if widgets:
+ self.showPage(widgets[-1], pages[-1], register=False)
+ else:
+ if self.qtAssistant.isOpen():
+ self.qtAssistant.closeAssistant()
+ return True
+ return False
+
+
+ def error(self, msg):
+ QtGui.QMessageBox.critical(self.parent, self.trUtf8('Qt assistant error'), msg)
+ return False
+
+
+ def initAssistant(self, parent):
+ """Inits QAssistant client if not already done"""
+ if self._isInited:
+ return
+ else:
+ self._isInited = True
+
+ try:
+ from PyQt4 import QtAssistant
+ except ImportError:
+ pass
+ else:
+ self.qtAssistant = QtAssistant.QAssistantClient('', parent)
+ self.qtAssistant.connect(
+ self.qtAssistant,
+ QtCore.SIGNAL('error(const QSting&)'),
+ self.error
+ )
+ self.qtAssistant.connect(
+ self.qtAssistant,
+ QtCore.SIGNAL('assistantClosed()'),
+ self.onAssistantClosed
+ )
+
+ L = QtCore.QStringList('-profile' ) << self.profile
+ self.qtAssistant.setArguments(L)
+
+
+ def showPage(self, parent, name, register=True):
+ """Shows a page
+ @param widget: widget to show the page for
+ @param name: name of the page
+ @param register: if True the widget will be registerd in the stack of open pages
+ """
+ self.initAssistant(parent)
+ if self.qtAssistant is None:
+ return self.error(self.trUtf8('Can not display help. Assistant is not installed'))
+
+ fpath = self.pages.get(name, None)
+ if fpath is None:
+ return self.error(self.trUtf8('Can not display help. No such page: ') + name)
+ if not os.path.isfile(fpath):
+ return self.error(self.trUtf8('Can not display help. Page not found: ') + '\n' + fpath)
+ if register and parent not in self._referers[0]:
+ self._referers[0].append(parent)
+ self._referers[1].append(name)
+ self.qtAssistant.showPage(fpath)
+ return True
+
+
+
+
+
+
+
+
\ No newline at end of file
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/__init__.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/__init__.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/__init__.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1 @@
+
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/areatips.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/areatips.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/areatips.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,465 @@
+# -*- coding: utf-8 -*-
+
+"""Customized tooltips
+"""
+
+# TODO:
+#
+# x. fix ComboBoxEditTip to act as promised
+# x. add classes to handle temTips for editBoxes (...)
+#
+# x. ComboBox trouble.
+#
+# XXX jopefuly fixed by adding an event.type().Leave handler to cancel toolTip display XXX
+#
+# y. The dropdown of a combobox does not seem to eat up events
+# An itemView underneath the dropdown still receives mouseMove events
+# and may pop up an itemTip. No idea what todo.
+#
+# y. there is a glitch with comboBoxes. When an itemTip is popped up
+# and the user clicks on it, the dropDown is closed, but the itemTip
+# not. For some reason the itemView eats the mouse message and
+# the itemTip never gets notified.
+#
+# x. there is a possible glitch aswell in itemTips when an itemView gets
+# disabled / hidden while an itemTip is scheduled for display. No way to
+# cancel the itemTip currently
+#
+#
+import os
+from PyQt4 import QtCore, QtGui
+
+__version__ = '0.0.1'
+#***************************************************************************
+#
+#***************************************************************************
+DEFAULT_SHOW_DELAY = 400
+
+#***************************************************************************
+#
+#***************************************************************************
+#code for the toolTip is taken from QToolTip.cpp
+
+class AreaTip(QtGui.QLabel):
+ """
+ Customized tooltip class to show tooltips for a specified area
+ under the mouse cursor.
+
+ """
+
+ def __init__(self, parent, showDelay=DEFAULT_SHOW_DELAY):
+ """constructor
+
+ @param parent parent of the tooltip
+ @showDelay delay in milliseconds before the tootliptp is shown
+ """
+
+
+ QtGui.QLabel.__init__(self, parent, QtCore.Qt.ToolTip)
+ self.hide()
+
+
+ self._currentAreaTipData = None
+ self._showTimer = QtCore.QTimer(self)
+ self._showDelay = showDelay
+
+ self.connect(
+ self._showTimer,
+ QtCore.SIGNAL("timeout()"),
+ self.onShowTip)
+
+
+
+ self.ensurePolished()
+ frameW = self.style().pixelMetric(
+ QtGui.QStyle.PM_ToolTipLabelFrameWidth,
+ None,
+ self
+ )
+ self.setMargin( 1 + frameW)
+ self.setFrameStyle(QtGui.QFrame.NoFrame)
+ self.setAlignment(QtCore.Qt.AlignLeft)
+ self.setIndent(1)
+
+ self.installEventFilter(self)
+
+ opacity = self.style().styleHint(
+ QtGui.QStyle.SH_ToolTipLabel_Opacity,
+ None,
+ self
+ )
+ self.setWindowOpacity( opacity / 255.0)
+ self.setPalette(QtGui.QToolTip.palette())
+
+
+
+ def showDelay(self):
+ """returns the current show delay"""
+ return self._showDelay
+
+ def setShowDelay(self, n):
+ """adjusts the show delay
+ @param n milliseconds to delay the popup of the tooltip
+ """
+ self._showDelay = n
+
+
+ def paintEvent(self, event):
+ """overwritten QWidget.paintEvent"""
+ p = QtGui.QStylePainter(self)
+
+ opt = QtGui.QStyleOptionFrame()
+ opt.init(self)
+ p.drawPrimitive(QtGui.QStyle.PE_PanelTipLabel, opt)
+ p.end()
+
+ QtGui.QLabel.paintEvent(self, event)
+
+
+
+ def eventFilter(self, obj, event):
+ """event filter for the tooltip"""
+
+ type_ = event.type()
+ if type_ == event.KeyPress or type == event.KeyRelease:
+ key = event.key()
+ modifiers = event.modifiers()
+ if modifiers & QtCore.Qt.KeyboardModifierMask or \
+ key == QtCore.Qt.Key_Shift or \
+ key == QtCore.Qt.Key_Control or \
+ key == QtCore.Qt.Key_Alt or \
+ key == QtCore.Qt.Key_Meta:
+ return False
+
+ # TODO: delegate mouseactions to the window underneath the cursor
+ elif type_ in (
+ event.Leave,
+ event.WindowActivate,
+ event.WindowDeactivate,
+ event.MouseButtonPress,
+ event.MouseButtonRelease,
+ event.MouseButtonDblClick,
+ event.FocusIn,
+ event.FocusOut,
+ event.Wheel,
+ ):
+ self.hideTip()
+
+ return False
+
+
+ def onShowTip(self):
+ """called when the tooltip is about to be displayed"""
+
+ if self._currentAreaTipData:
+ pt, text, rc = self._currentAreaTipData
+ pos = QtGui.QCursor().pos()
+ if not rc.contains(pos):
+ return
+
+ desktop = QtGui.QApplication.desktop()
+ if desktop.isVirtualDesktop():
+ scr = desktop.screenNumber(pt)
+ else:
+ scr = desktop.screenNumber(self.parent())
+
+ if os.name == "mac":
+ screen = desktop.availableGeometry(scr)
+ else:
+ screen = desktop.screenGeometry(scr)
+
+ self.setText(text)
+ fm = QtGui.QFontMetrics(self.font())
+ extra = QtCore.QSize(1, 0)
+ # Make it look good with the default ToolTip font on Mac,
+ # which has a small descent.
+ if fm.descent() == 2 and fm.ascent() >= 11:
+ extra.setHeight(extra.height() + 1)
+
+ self.resize(self.sizeHint() + extra)
+
+ self.setWordWrap(QtCore.Qt.mightBeRichText(text))
+ self.move(pt)
+ self.show()
+
+
+ def cancel(self):
+ if self._showTimer.isActive():
+ self._showTimer.stop()
+
+
+ def hideTip(self):
+ """hides the tooltip"""
+ self.cancel()
+ self.hide()
+
+
+ def showText(self, pt, text, rc):
+ """shows the tooltip
+
+ @param pt: (global coordinates) point to show the tooltip at (left/top)
+ @param text: text to show (if emtpty string the tooltip is not shown)
+ @param rc: (global coordinates) bounding rect the tooltip is assigned to
+
+ """
+ self._currentAreaTipData = (pt, text, rc)
+ self.hideTip()
+ if not text.isEmpty():
+ self._showTimer.start(self._showDelay)
+
+
+ def showItemTip(self, itemView, index):
+ """Shows an item tip for item views (QTreeView, QListView ....)
+ for the specified index if necessary.
+
+ @param itemView: item view to display the tip for
+ @param index: QModelIndex of the item to display the tip for
+
+ Note: the tolltip will only be displayed if the text of the item is truncated
+ or the item is not entirely visible.
+ """
+ rc = self.getTruncatedItemRect(itemView, index)
+ if rc is not None:
+ text = index.data().toString()
+ if not text.isEmpty() and itemView.hasFocus():
+ self.showText(rc.topLeft(), text, rc)
+
+
+ def getTruncatedItemRect(self, itemView, index):
+ """Helper method. Returns the rect of a truncted item in an item view
+ @param itemView: QAbstractItemView
+ @param index: index of the item
+ @return: (global coordinates QRect) if the item is truncated, None otherwise
+ @note: this method is callled by showItemTip() to determine wether to
+ display a toolTip for an item or not. Overwrite to customize
+ """
+ viewport = itemView.viewport()
+ rcCli = viewport.contentsRect()
+ rcActual = itemView.visualRect(index)
+ rcDesired = QtCore.QRect(
+ rcActual.topLeft(),
+ itemView.sizeHintForIndex(index)
+ )
+ if not rcActual.contains(rcDesired, False) or \
+ not rcCli.contains(rcDesired, False):
+
+ rcActual.moveTo( viewport.mapToGlobal(rcActual.topLeft()) )
+ return rcActual
+
+
+#****************************************************************************
+#
+#****************************************************************************
+class ItemTips(QtCore.QObject):
+ """Equips an itemView (QTreeView, QListView ....) with tooltips for truncated items (item tips)
+ """
+
+ def __init__(self, itemView, showDelay=DEFAULT_SHOW_DELAY):
+ """
+ @param itemView: itemView to equip with item tips
+ param showDelay: delay in (miliseconds) after wich to show a tooltip for an item
+ """
+ QtCore.QObject.__init__(self, itemView)
+
+ self.areaTip = AreaTip(itemView, showDelay=showDelay)
+ self.itemView = itemView
+
+ itemView.setMouseTracking(True) # enshure we get mouseMove messages
+ itemView.viewport().installEventFilter(self)
+
+
+ def eventFilter(self, obj, event):
+ """Event filter for the itemTip"""
+ if event.type() == event.MouseMove:
+ if self.areaTip.isHidden():
+
+ # hope this check fixes a glitch with comboBoxes. Sometimes am itemTip
+ # did pop up when the combos dropdown was closed.
+ if not self.itemView.isHidden() and self.itemView.isEnabled() and self.itemView.underMouse():
+ index = self.itemView.indexAt(event.pos())
+ if index.isValid():
+ self.areaTip.showItemTip(self.itemView, index)
+ if event.type() == event.Leave:
+ self.areaTip.cancel()
+
+ return False
+
+
+ def setEnabled(self, flag):
+ self.areaTip.setEnabled(flag)
+ pass
+
+ def isEnabled(self):
+ self.areaTip.isEnabled()
+
+ def getShowDelay(self):
+ """returns the current show delay"""
+ return self.areaTip.getShowDelay()
+
+ def setShowDelay(self, n):
+ """adjusts the show delay
+ @param n milliseconds to delay the popup of the tooltip
+ """
+ return self.areaTip.setShowDelay(delay)
+
+#****************************************************************************
+#
+#****************************************************************************
+class ComboBoxEditTips(QtCore.QObject):
+ """Equips a QComboBox with a toolTip popping up when the text in its edit box is truncated
+
+ @note: this class does currently not quite what it is supposed todo. A normal toolTip is
+ sisplayed instead of a toolTip that covers the editBox. This may change in future versions.
+ """
+
+ def __init__(self, comboBox, showDelay=DEFAULT_SHOW_DELAY):
+ QtCore.QObject.__init__(self, comboBox)
+
+ self.connect(
+ comboBox,
+ QtCore.SIGNAL('currentIndexChanged(const QString &)'),
+ self._adjustToolTip
+ )
+
+ comboBox.installEventFilter(self)
+ self.comboBox = comboBox
+
+
+ def eventFilter(self, obj, event):
+ if event.type() == event.Resize:
+ self._adjustToolTip(self.comboBox.currentText())
+ return False
+
+ def _adjustToolTip(self, text):
+ fm = self.comboBox.fontMetrics()
+ style = self.comboBox.style()
+ rc = self.comboBox.contentsRect()
+ rc2 = style.subControlRect(
+ style.CC_ComboBox,
+ QtGui.QStyleOptionComboBox(),
+ style.SC_ComboBoxEditField,
+ )
+
+ cx = rc.width() + rc2.width()
+ w = fm.width(text)
+ if w > cx -2:
+ self.comboBox.setToolTip(text)
+ else:
+ self.comboBox.setToolTip('')
+
+
+
+
+#********************************************************************************************
+# some test guis
+#********************************************************************************************
+def _testItemModel():
+ """Tests itemTips for a QTableView"""
+
+ import sys
+
+
+ class TestModel(QtCore.QAbstractTableModel):
+
+
+ def __init__(self, table):
+ QtCore.QAbstractTableModel.__init__(self, table)
+
+ self.__table = table
+ self.__columns = ('foo', "foo")
+ self.__rows = ['foo'*10 for i in range(10)]
+
+ self.areaTip = AreaTip(table)
+
+
+ def hasIndex(self, row, column):
+ """QAbstractTableModel.hasIndex() implementation"""
+ if -1 < column <= len(self.__columns):
+ if -1 < row < len(self.__rows):
+ return True
+ return False
+
+ def rowCount(self, parent):
+ """QAbstractTableModel.rowCount() implementation"""
+ return len(self.__rows)
+
+
+ def columnCount(self, parent):
+ """QAbstractTableModel.columnCount() implementation"""
+ return len(self.__columns)
+
+
+ def headerData(self, section, orientation, role):
+ """QAbstractTableModel.headerData() implementation"""
+ if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Horizontal:
+ return QtCore.QVariant(self.__columns[section])
+ return QtCore.QVariant()
+
+
+ def data(self, index, role):
+ """QAbstractTableModel.data() implementation"""
+
+ if self.hasIndex(index.row(), index.column()):
+ if role == QtCore.Qt.DisplayRole:
+ return QtCore.QVariant(self.__rows[index.column()])
+
+ # display tooltip if text does not fit column width
+ # or item is not entirely visible
+ elif role == QtCore.Qt.ToolTipRole:
+ self.areaTip.showItemTip(self.__table, index)
+
+
+ return QtCore.QVariant()
+
+ app = QtGui.QApplication(sys.argv)
+ dlg = QtGui.QWidget()
+
+ grid = QtGui.QHBoxLayout(dlg)
+
+ tv = QtGui.QTableView(dlg)
+ grid.addWidget(tv)
+
+ tv.setModel(TestModel(tv))
+
+ dlg.show()
+ res = app.exec_()
+ sys.exit(res)
+
+
+
+def _testComboBox():
+ """Tests itemTips for a QCombobox"""
+
+
+ import sys
+
+ app = QtGui.QApplication(sys.argv)
+ w = QtGui.QWidget()
+
+ b = QtGui.QGridLayout(w)
+ c = QtGui.QComboBox(w)
+ c.setMinimumContentsLength(5)
+ b.addWidget(c)
+
+ itemTips = ItemTips(c.view())
+
+
+ for i in range(10):
+ c.addItem('loooooooooooooooooooooooooooooong-itemText-%s' % i)
+
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
+
+
+
+#***********************************************************************
+#
+#***********************************************************************
+if __name__ == "__main__":
+ pass
+ #_testItemModel()
+ #_testComboBox()
+
+
+
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/checkarraywrap.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/checkarraywrap.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/checkarraywrap.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,93 @@
+"""wrapper class for handling checkboxes (...) as bit array
+
+
+CONST_1 = 1
+CONST_2 = 2
+CONST_3 = 4
+
+checks = {
+ CONST_1: ckeckbox1,
+ CONST_2: checkbox2,
+ CONST_3: cjeckbox3,
+
+ }
+
+
+flags = CheckArray(checks, initvalue=CONST_1 | CONST_2)
+
+flags |= CONST_3 # check 3rd checkbox
+flags.showChecks(CONST_1 | CONST_2) # hide 3rd checkbox
+
+"""
+
+from PyQt4 import QtCore
+#*******************************************************
+#
+#*******************************************************
+class CheckArrayWrap(QtCore.QObject):
+
+ def __init__(self, parent, checks, value=None):
+ QtCore.QObject.__init__(self, parent)
+ self._checks = checks
+
+ if value is not None:
+ self.setChecks(value)
+
+ ######################################
+ ## public methods
+ ######################################
+ def getValue(self):
+ value = 0
+ for const, ck in self._checks.items():
+ if ck.isChecked():
+ value |= const
+ return value
+
+
+ def setChecks(self, flags):
+ for const, ck in self._checks.items():
+ if flags & const:
+ ck.setCheckState(QtCore.Qt.Checked)
+ else:
+ ck.setCheckState(QtCore.Qt.Unchecked)
+ flags &= ~const
+ if not flags:
+ break
+
+
+ def showChecks(self, flags):
+ for const, ck in self._checks.items():
+ if flags & const:
+ check.control.setEnabled(True)
+ check.control.show()
+ else:
+ check.control.setEnabled(False)
+ check.control.hide()
+ flags &= ~const
+ if not flags:
+ break
+
+
+ def __or__(self, n):
+ value = self.getValue() | n
+ self.setChecks(value)
+ return self
+ def __ior__(self, n): return self.__or__(n)
+ def __ror__(self, n): return self.__or__(n)
+
+ def __and__(self, n):
+ value = self.getValue() & n
+ self.setChecks(value)
+ return self
+ def __iand__(self, n): return self.__and__(n)
+ def __rand__(self, n): return self.__and__(n)
+
+ def __xor__(self, n):
+ value = self.getValue() ^ n
+ self.setChecks(value)
+ return self
+ def __ixor__(self, n): return self.__xor__(n)
+ def __rxor__(self, n): return self.__xor__(n)
+
+
+
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/colorbutton.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/colorbutton.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/colorbutton.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,238 @@
+"""pyQt4 ColorButton with drag and drop support
+
+mostly taken from
+[http://api.kde.org/4.0-api/kdelibs-apidocs/kdeui/html/classKColorButton.html]
+with some minor adjustements
+"""
+
+import sys, os
+
+#--> rel import hack
+d = os.path.dirname(os.path.dirname(__file__))
+sys.path.insert(0, d)
+
+import qtools
+
+sys.path.pop(0)
+del d
+#<-- rel import hack
+
+
+from PyQt4 import QtCore, QtGui
+
+__author__ = 'Juergen Urner'
+__email__ = 'jU...@ar...'
+__version__ = '0.1.0'
+#************************************************************************
+#
+#************************************************************************
+class ColorButtonWrap(QtCore.QObject):
+ """Event filter to transform a QPushButton into a color button
+
+ The button will popup a color selection dialog when hit ++ the button supports
+ drag / drop of colors.
+
+ @signal colorChanged(const QColor*): emited when the user changes the color of
+ the button via color dialog or color drop.
+ """
+
+ def __init__(self, button, color=None, fitColor=True):
+ """
+ @param button: button to wrap
+ @param color: initial color. If None, initial color is black
+ @fitColor: if True the color shown is adjusted to fit the contents of the button
+ """
+ QtCore.QObject.__init__(self, button)
+
+ self.button = button
+ self._color = color or QtGui.QColor('black')
+ self.fitColor = fitColor
+
+ button.setAcceptDrops(True)
+ button.installEventFilter(self)
+ self.connect(button, QtCore.SIGNAL('clicked()'), self.chooseColor)
+
+
+ def _initStyleOption(self, opt):
+ opt.initFrom(self.button)
+ opt.text.clear()
+ opt.icon = QtGui.QIcon()
+ opt.features = QtGui.QStyleOptionButton.None
+
+
+ def eventFilter(self, obj, event):
+ """Event filter for the button wrapped"""
+
+ eventType = event.type()
+
+ if eventType == event.Paint:
+ painter = QtGui.QPainter(self.button)
+
+ # draw bevel
+ opt = QtGui.QStyleOptionButton()
+ self._initStyleOption(opt)
+ self.button.style().drawControl(QtGui.QStyle.CE_PushButtonBevel, opt, painter, self.button)
+
+ # draw color box
+ #First, sort out where it goes
+ labelRect = self.button.style().subElementRect(QtGui.QStyle.SE_PushButtonContents, opt, self.button)
+ shift = self.button.style().pixelMetric(QtGui.QStyle.PM_ButtonMargin)
+ labelRect.adjust(shift, shift, -shift, -shift);
+ x, y, w, h = labelRect.getRect()
+
+ if self.button.isChecked() or self.button.isDown():
+ x += self.button.style().pixelMetric(QtGui.QStyle.PM_ButtonShiftHorizontal)
+ y += self.button.style().pixelMetric(QtGui.QStyle.PM_ButtonShiftVertical)
+ if self.button.isEnabled():
+ fillCol = self.getColor()
+ else:
+ fillCol = self.button.palette().color(self.button.backgroundRole())
+
+ QtGui.qDrawShadePanel(painter, x, y, w, h, self.button.palette(), True, 1, None)
+ if fillCol.isValid():
+ painter.fillRect( x+1, y+1, w-2, h-2, fillCol)
+
+ if self.button.hasFocus():
+ focusRect = self.button.style().subElementRect(QtGui.QStyle.SE_PushButtonFocusRect, opt, self.button)
+ focusOpt = QtGui.QStyleOptionFocusRect()
+ focusOpt.initFrom(self.button)
+ focusOpt.rect = focusRect
+ focusOpt.backgroundColor = self.button.palette().background().color()
+ self.button.style().drawPrimitive(QtGui.QStyle.PE_FrameFocusRect, focusOpt, painter, self.button)
+ return True
+
+ elif eventType == event.MouseMove:
+ if not self._mouse_pressed:
+ return False
+
+ if event.buttons() & QtCore.Qt.LeftButton:
+ if (self._mousepress_pos - event.pos()).manhattanLength() > QtGui.QApplication.startDragDistance():
+ self._mouse_pressed = False
+ self.button.setDown(False)
+ drag = QtGui.QDrag(self.button)
+ data = QtCore.QMimeData()
+ data.setColorData(QtCore.QVariant(self._color))
+ drag.setMimeData(data)
+ drag.start(QtCore.Qt.CopyAction)
+ return True
+
+ elif eventType == event.MouseButtonPress:
+ self._mousepress_pos = QtCore.QPoint(event.pos())
+ self._mouse_pressed = True
+ return False
+
+ elif eventType == event.MouseButtonRelease:
+ self._mouse_pressed = False
+ return False
+
+ elif eventType == event.DragEnter:
+ if event.mimeData().hasColor():
+ event.accept()
+ else:
+ event.ignore()
+ return True
+
+ elif eventType == event.DragMove:
+ if event.mimeData().hasColor():
+ event.accept()
+ else:
+ Event.ignore()
+ return True
+
+ elif eventType == event.Drop:
+ if event.mimeData().hasColor():
+ v = event.mimeData().colorData()
+ self.setColor( QtGui.QColor(v.toString() ) )
+ else:
+ event.ignore()
+ return True
+
+
+ # TODO: copy & paste
+ if eventType == event.KeyPress:
+
+ if qtools.isStandardKeyEvent(event, QtGui.QKeySequence.Copy):
+ mimeData = QtCore.QMimeData()
+ mimeData.setColorData(QtCore.QVariant(self.getColor() ) )
+ QtGui.QApplication.clipboard().setMimeData(mimeData, QtGui.QClipboard.Clipboard)
+ elif qtools.isStandardKeyEvent(event, QtGui.QKeySequence.Paste):
+ mimeData = QtGui.QApplication.clipboard().mimeData(QtGui.QClipboard.Clipboard)
+ if mimeData.hasColor():
+ v = mimeData.colorData()
+ self.setColor( QtGui.QColor(v.toString() ) )
+
+
+
+
+
+ """
+ void KColorButton::keyPressEvent( QKeyEvent *e )
+ {
+ int key = e->key() | e->modifiers();
+
+ if ( KStandardShortcut::copy().contains( key ) ) {
+ QMimeData *mime=new QMimeData;
+ KColorMimeData::populateMimeData(mime,color());
+ QApplication::clipboard()->setMimeData( mime, QClipboard::Clipboard );
+ }
+ else if ( KStandardShortcut::paste().contains( key ) ) {
+ QColor color=KColorMimeData::fromMimeData( QApplication::clipboard()->mimeData( QClipboard::Clipboard ));
+ setColor( color );
+ }
+ else
+ QPushButton::keyPressEvent( e );
+ }
+ """
+
+ return False
+
+
+ #############################################
+ ## methods
+ #############################################
+ def chooseColor(self):
+ """Pops up a color selection dialog to adjust the color of the button"""
+ color = QtGui.QColorDialog.getColor(self._color, self.button)
+ if color.isValid():
+ self.setColor(color)
+ self.emit(QtCore.SIGNAL('colorChanged(const QColor*)'), color)
+
+ def getColor(self):
+ """Returns the color of the button
+ @return: QColor
+ """
+ return self._color
+
+ def setColor(self, color):
+ """Sets the color of the button
+ @param color: QColor
+ """
+ self._color = color
+ self.button.update()
+ self.emit(QtCore.SIGNAL('colorChanged(const QColor*)'), color)
+
+
+#********************************************************************
+#
+#********************************************************************
+if __name__ == '__main__':
+ import sys
+
+ app = QtGui.QApplication(sys.argv)
+ w = QtGui.QWidget(None)
+ box = QtGui.QVBoxLayout(w)
+ colors = ('yellow', 'red', 'green', 'blue')
+ for color in colors:
+ bt = QtGui.QPushButton(w)
+ wrap = ColorButtonWrap(bt, QtGui.QColor(color))
+ box.addWidget(bt)
+
+
+
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
+
+
+
+
\ No newline at end of file
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/compactpatwrap.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/compactpatwrap.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/compactpatwrap.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,576 @@
+
+"""module to handles compacting of filepaths and urls
+
+ @author Juergen Urner
+ """
+
+import operator
+import os
+import re
+
+import ntpath
+import posixpath
+import macpath
+
+from PyQt4 import QtCore, QtGui
+
+# TODO:
+#
+# x. builtin type() is shaddowed in some methods
+# x. rename DEFAULT to DEFAULT_TYPE
+
+#*******************************************************************************
+# compacts a filepath to fit into a desired width
+# As measuring function any function may be passed that returns True if a path
+# is short enough, False otherwise. Default is len()
+#
+#
+# IDEA:
+#
+# weight path components by priority, 0 is lowest.
+#
+# 2 0 1 3
+# mypath = "aaa/bb/ccc/ddd"
+#
+# genearte a sort matrix [[nPriority0, indexComponent0, component0], [...]]
+# so we only have to flip the matrix to either get the priority order of the
+# components and eat components from martx[0] to matrix[N] or the restore
+# the original component order
+#
+# matrix = [[2, 0, "aaa"], [0, 1, "bbb"], [1, 2, "ccc"], [3, 3, "ddd"]]
+# matrix.sort()
+# matrix = [[0, 1, "bbb"], [1, 2, "ccc"], [2, 0, "aaa"], [3, 3, "ddd"]]
+#
+# I am shure that so. with better algo skills than me can come up with a nicer
+# solution to the problem. I would like to see that.
+#
+#**********************************************************************************
+
+#**************************************************************************************
+# helpers
+#**************************************************************************************
+_drive_pat = re.compile(r'([a-zA-Z]\:)(?=/|\\|\Z)')
+def is_drive(fpath):
+ return bool(_drive_pat.match(fpath))
+
+_host_pat = re.compile(r'([^:/?#]+:[^/]*///?)')
+def is_host(fpath):
+ return bool(_host_pat.match(fpath))
+
+def is_root(fpath):
+ return fpath.startswith("/")
+
+def is_mac_root(fpath):
+ if fpath:
+ return ':' in fpath and fpath[0] != ':'
+ return False
+
+if os.path.__name__.startswith("nt"):
+ DEFAULT = "nt"
+elif os.path.__name__.startswith("mac"):
+ DEFAULT = "mac"
+elif os.path.__name__.startswith("posix"):
+ DEFAULT = "posix"
+else:
+ DEFAULT = os.path.__name__
+
+PATH_MODULES = { # modules to handle path actions for path types
+ 'nt': ntpath,
+ 'posix': posixpath,
+ 'mac': macpath,
+ 'url': posixpath,
+ DEFAULT: os.path,
+ }
+
+#**************************************************************************************
+#
+#**************************************************************************************
+class PathCompacter(object):
+ """
+ implementation of the compact path algorithm.
+
+ The class is available as compactPath() function in the module.
+ For a description of parameters see PathCompacter.__call__()
+
+ @note: you do not have to use this class directly. On the module level
+ its functionality is available as function compactPath()
+ """
+
+ ELLIPSIS = "..."
+ PARDIR = ".."
+ DOT = "."
+
+ def __init__(self):
+ """
+ constructor
+ """
+ self.root = ""
+ self.path_module = None
+
+
+ def chewComponent(self, cpn, fromLeft=True):
+ """
+ shorten a single path component by one char
+
+ @param cpn the component to to be shortened
+ @keyparam fromLeft if True the component is shortened on the left side,
+ if False on the right
+ """
+ if fromLeft:
+ while True:
+ cpn = cpn[3: ]
+ if not cpn:
+ yield self.PARDIR
+ break
+
+ cpn = self.PARDIR + cpn
+ yield cpn
+
+ else:
+ while True:
+ cpn = cpn[ :-3]
+ if not cpn:
+ yield self.PARDIR
+ break
+ cpn = cpn + self.PARDIR
+ yield cpn
+
+
+ def getMatrix(self, n):
+ """
+ generates a sort matrix of lenght (n)
+ """
+ if n == 0:
+ return []
+
+ # ...enshure last item in range is last item in the matrix
+ if n % 2:
+ rng = range(n)
+ else:
+ rng = range(n -1)
+
+ start = len(rng) / 2
+ out = []
+ for i in rng:
+ if i == start:
+ out.append([0, ])
+ elif i < start:
+ out.append([ (start - i) *2 - 1, ])
+ else:
+ out.append([ (i - start) *2, ])
+
+ if not n % 2:
+ out.append([len(out)])
+ return out
+
+
+ def matrixToPath(self, matrix):
+ """
+ private method to generate a filepath from a sort matrix
+ """
+ getter = operator.itemgetter
+ out = []
+
+ matrix.sort(key=getter(1))
+ for i in matrix:
+ out.append(i[2])
+
+ matrix.sort()
+
+ if self.type == "url":
+ return self.root + "/".join(out)
+ elif self.type == "nt":
+ return self.path_module.join(self.root, *out)
+ elif self.type == "posix":
+ return self.path_module.join(self.root, *out)
+ elif self.type == "mac":
+ return self.path_module.join(self.root, *out)
+ return self.path_module.join(*out)
+
+
+ def matrixFromPath(self, fpath):
+ """
+ private method to convert a filepath into a sort matrix
+ """
+ arrPath = self._path_to_list(fpath)
+ if not arrPath:
+ return []
+
+ self.root = ""
+ if self.type == "nt":
+ if is_drive(arrPath[0]):
+ self.root = arrPath[0]
+ arrPath.pop(0)
+ elif self.type == "posix":
+ if is_root(arrPath[0]):
+ self.root = arrPath[0]
+ arrPath.pop(0)
+ elif self.type == "mac":
+ if is_mac_root(arrPath[0]):
+ self.root = arrPath[0]
+ arrPath.pop(0)
+ elif self.type == "url":
+ host = arrPath[0] + "//"
+ if is_host(host):
+ self.root = host
+ arrPath.pop(0)
+
+ matrix = self.getMatrix(len(arrPath))
+ i = 0
+ for item in matrix:
+ item.append(i)
+ item.append(arrPath[i])
+ i += 1
+
+ return matrix
+
+
+ def _path_to_list(self, fpath):
+ head = fpath
+ out = []
+ # taken from: http://www.jorendorff.com/articles/python/path/
+ while head != self.path_module.curdir and head != self.path_module.pardir:
+ prev = head
+ head, tail = self.path_module.split(head)
+ if head == prev: break
+ out.append(tail)
+ if head:
+ out.append(head)
+ out.reverse()
+ return out
+
+
+ def __call__(self, fpath, w, measure=len, max_pardirs=2, type=DEFAULT):
+ """
+ compacts a filepath or url to fit into the desired width
+
+ @param fpath the filepath to be compacted
+ @param w the desired width the filepath should fit into
+ @keyparam measure function to measure the length of the filepath.
+ Default is len() but may be any other function that takes a filepath as
+ argument and returns its length as undigned int.
+ @keyparam max_pardirs maximum number of compacted parent dirs ("../") allowed
+ in the compacted path. Must be > 0.
+ @keyparam type use this explicitely specify the type of path passed.
+ Can be "posix", "nt", "mac" or "url" or DEFAULT. DEFAULT processes the
+ path with whatever os.path currently is.
+
+ """
+ if max_pardirs < 1:
+ raise PathError("max_pardirs < 1 is not allowed")
+
+ if not fpath:
+ return ""
+ if measure(fpath) < w:
+ return fpath
+
+ self.type, self.path_module = type, PATH_MODULES[type]
+ matrix = self.matrixFromPath(fpath)
+ if not matrix:
+ return "" # error here, the pattern does not match anything
+ matrix.sort()
+
+ # process our sort matrix
+ i = 0
+ while True:
+
+ if len(matrix) > 2:
+ # ...chew next component till it's exhausted
+ item = matrix[i]
+ for cpn in self.chewComponent(item[2], fromLeft=False):
+ item[2] = cpn
+ path = self.matrixToPath(matrix)
+ if measure(path) < w:
+ return path
+
+ if cpn == self.PARDIR:
+ i += 1
+ break
+
+ # ...pop 1 pardir if necessary
+ if i > max_pardirs or i >= len(matrix) -1:
+ matrix.pop(0)
+ matrix[0][2] = self.ELLIPSIS
+ i -= 1
+ path = self.matrixToPath(matrix)
+ if measure(path) < w:
+ return path
+
+ else:
+ # finalize
+ # 1. chew root away
+ if self.root:
+ for cpn in self.chewComponent(self.root, fromLeft=True):
+ self.root = cpn
+ if self.root == self.PARDIR:
+ if not matrix:
+ if measure(self.PARDIR) < w:
+ return self.PARDIR
+ if measure(self.DOT) < w:
+ return self.DOT
+ return ""
+ self.root = ""
+ break
+ path = self.matrixToPath(matrix)
+ if measure(path) < w:
+ return path
+
+ path = self.matrixToPath(matrix)
+ if measure(path) < w:
+ return path
+
+ # 2. chew filename away
+ if len(matrix) == 2:
+ component = matrix[0][2] + matrix[1][2]
+ if measure(component) < w:
+ return component
+ else:
+ component = matrix[0][2]
+
+ for cpn in self.chewComponent(component, fromLeft=True):
+ if measure(cpn) < w:
+ return cpn
+
+ if cpn == self.PARDIR:
+ if measure(self.DOT) < w:
+ return self.DOT
+ break
+
+ # done it
+ break
+
+ return ""
+
+
+#********************************************
+# init PathCompacter() as function
+#********************************************
+compactPath = PathCompacter()
+#********************************************************************************
+# lightweight wrapper class for QLabels to display a compactable filepath or url
+# the class is designed as a wrapper not a derrived class so it does not get in the
+# way with QtDesigner
+#
+# problem to tackle is that every setText() triggers a resize of the whole layout
+# so the filepath can't be adjusted in resizeEvent() (recursive). We handle
+# paintEvent() instead and draw the label from scratch.
+#
+# similar wrappers can be easily implemented for QMenuItem() and QListView()
+# header controls (...).
+#
+# Usage hint:
+# if used on statusbars you should adjust the stretch factor, otherwise
+# the label might appear invisible, 'cos it may not get resized.
+#
+# The label has a minimum size of 0 by default. To force a minimum
+# size set any initial text to the label wrapped. Effect is 1. the text will not be
+# displayed ++ the label will not get resized any smaller than this text.
+#
+#*******************************************************************************
+class PathLabelWrap(object):
+ """
+ class wrapping a QLabel to display a filepath or url that is compacted on
+ resizing of the label
+ """
+
+ def __init__(self, label, fpath="", prefix="", max_pardirs=1, type=DEFAULT):
+ """
+ constructor
+
+ @param label an initialized QLabel to wrap compactPath functionality around
+ @param fpath the filepath or url the label should display
+ @keyparam prefix: chars to be used as fixed prefix of the path like: "file: my/path/here"
+ @keyparam max_pardirs maximum number of compacted parent dirs ("../") allowed
+ in the compacted path. Must be > 0.
+ @keyparam type: use this to explicitely specify the type of path specified.
+ Can be "nt", "posix", "mac", "url" or DEFAULT to use whatever type of path
+ os.path currently handles.
+
+ Note: currently the wrapper does not know how to display text for disabled state.
+ So disabling the underlaying QLabel will not result in the desired effect.
+ """
+ self.label = label
+ self.fpath = fpath
+ self.prefix = prefix
+ self.max_pardirs= max_pardirs
+ self.type = type
+
+ self.label.paintEvent = self.onPaint # overwrite
+ # for testing:
+ #self.label.setPaletteBackgroundColor(qt.QColor(10, 255, 22))
+
+
+ def onPaint(self, event):
+ """
+ overwritten method to handle painting of the filepath
+ """
+
+ # draw the label from scratch
+ # TODO: check if there is an easier processing via QStyle.drawControl()
+ fm = self.label.fontMetrics()
+ rc = self.label.rect()
+ frameW = self.label.frameWidth()
+ indent = self.label.indent()
+ if indent < 0: # see Qt docs: label.indent()
+ if frameW > 0:
+ indent = fm.width("x") /2
+ else:
+ indent = 0
+ rc.adjust(frameW + indent, frameW , -(frameW + indent), -frameW)
+
+ w = rc.width()
+ if self.prefix:
+ w -= fm.width(self.prefix)
+
+ fpath = compactPath(
+ self.fpath,
+ w,
+ measure=fm.width,
+ max_pardirs=self.max_pardirs,
+ type=self.type
+ )
+
+ painter = QtGui.QPainter(self.label)
+ painter.eraseRect(self.label.rect())
+ self.label.drawFrame(painter)
+ # TODO: draw enabled/disabled text
+ # textColor is already ok but I haven't found a way to draw disabled text
+ # embossed
+ # if self.label.isEnabled():
+ # else:
+ if self.prefix:
+ painter.drawText(rc, self.label.alignment(), '%s%s' % (self.prefix, fpath))
+ else:
+ painter.drawText(rc, self.label.alignment(), fpath)
+
+
+ def setPath(self, fpath, prefix=None, max_pardirs=None, type=None):
+ """
+ sets the filepath or url to be displayed
+
+ If any of the keyword params is not None, the according property is adjusted to the specified value
+ """
+ if prefix is not None:
+ self.prefix = prefix
+ if max_pardirs is not None:
+ self.max_pardirs = max_pardirs
+ if type is not None:
+ self.type = type
+ self.fpath = fpath
+ self.label.update()
+
+ def getPath(self):
+ """
+ retrieves the (uncompacted) filepath or url
+ """
+ return self.fpath
+
+#******************************************************************************
+# tests
+#******************************************************************************
+if __name__ == "__main__":
+
+ def testGui():
+
+ CAPTION = "compactPath - [%s]"
+ PATHS = (
+ ('posix', "/my/very/long/path/containing/many/compoponents/here.txt"),
+ ('nt', "c:\\my\\very\\long\\path\\containing\\many\\compoponents\\here.txt"),
+ ('mac', "my:very:long:path:containing:many:compoponents:here.txt"),
+ ('url', "http://my/very/long/path/containing/many/compoponents/here.txt"),
+ )
+ BLINDTEXT = "xxxxxxxxxx" # this is set as text to the labels to force a minimum size. Adjust to your needs
+
+ class TestGui(QtGui.QMainWindow):
+ """test gui"""
+
+ def __init__(self):
+ QtGui.QMainWindow.__init__(self)
+
+ self.mainWidget = QtGui.QWidget(self)
+ self.setCentralWidget(self.mainWidget)
+
+ # NOTE: all the sep="" params are only thrown in for test purposes.
+ #
+ # Too bad, the label in the caption bar seems not to be available in Qt. So there is no way
+ # to adjust it dynamically. So init to some fixed width. Note that the width of the caption
+ # bar label has no effect on the GUIs width.
+ #
+ pathType, fpath = PATHS[0]
+ fpath = compactPath(fpath, 50 - len(CAPTION), type=pathType)
+ self.setWindowTitle(CAPTION % fpath)
+
+ # throw labels into the Gui
+ layout = QtGui.QVBoxLayout(self.centralWidget())
+ frameStyle = QtGui.QLabel.Sunken | QtGui.QLabel.Box
+ self.pathLabels = []
+ for pathType, fpath in PATHS:
+ label = QtGui.QLabel(BLINDTEXT, self.mainWidget)
+ w = PathLabelWrap(label, fpath=fpath, prefix=pathType + ': ', type=pathType)
+ label.setFrameStyle(frameStyle)
+ layout.addWidget(label)
+
+ # just a test label to see if our owner drawn labels are ok
+ self.labelTest = QtGui.QLabel("just a test", self.mainWidget)
+ self.labelTest.setFrameStyle(frameStyle)
+ layout.addWidget(self.labelTest)
+
+ layout.addItem(QtGui.QSpacerItem(0, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Expanding))
+
+ # add another one on the statusbar
+ self.status = self.statusBar()
+ self.statusLabelPosix = PathLabelWrap(QtGui.QLabel(BLINDTEXT, self.status), fpath=fpath)
+ self.status.addWidget(QtGui.QLabel('status::', self.status))
+ self.status.addWidget(self.statusLabelPosix.label, 10)
+ self.status.addWidget(QtGui.QLabel('foo-bar', self.status))
+
+ def styleChange(self, oldStyle):
+ """styleChange handler"""
+ self.update()
+
+ import sys
+ a = QtGui.QApplication(sys.argv)
+ QtCore.QObject.connect(a,QtCore.SIGNAL("lastWindowClosed()"),a,QtCore.SLOT("quit()"))
+ w = TestGui()
+ w.show()
+ a.exec_()
+
+ ##
+ testGui()
+
+
+
+ def test():
+ """
+ test compactPath()
+ """
+
+ def compact(path, start, stop, type=DEFAULT):
+ for i in range(start +1, stop, -1):
+ p = compactPath(path, i, max_pardirs=1, type=type)
+ print i, len(p), repr(p)
+
+ def test1():
+ path = "a:\\eeeeee\\fff\\foobarbaz\\ddd"
+ compact(path, len(path), 0, type="nt")
+ test1()
+
+ def test2():
+ path = "/home/eeeeee/fff/foobarbaz/ddd"
+ compact(path, len(path), 0, type="posix")
+ test2()
+
+ def test3():
+ path = "home:eeeeee:fff:foobarbaz:ddd"
+ compact(path, len(path), 0, type="mac")
+ test3()
+
+ def test4():
+ path = "home://eeeeee/fff/foobarbaz/ddd"
+ compact(path, len(path), 0, type="url")
+ test4()
+
+ ##
+ #test()
+
+
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/dragtool.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/dragtool.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/dragtool.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,205 @@
+"""DraggableTool widget"""
+
+
+from PyQt4 import QtCore, QtGui
+
+__version__ = '0.0.4'
+#**********************************************************************
+#
+#**********************************************************************
+class DragToolEventFilter(QtCore.QObject):
+ """helper widget to handle draging of bitmaps as cursor
+
+ @note: the widget has to be visible and at least 1x1 in size, otherwise Qt refuses to
+ pass the mouse capture over to the widget
+ """
+
+
+ def __init__(self, button, cursor=None, releaseOnClick=False):
+ """constructor
+
+ @arg parent: parent of the grabber widget
+ @arg cursor: QCursor
+ @arg grab_cursor: if 'grab_cursor' is True the user may drag the pixmap around
+ without holding the left mouse button. If False the cursor is released as soon as
+ the user releases the the left mouse button. In both cases dragging can be endet
+ by hitting the escape key. In "grab_cursor" mode dragging may be endet aswell by
+ hitting the right mouse button.
+
+ """
+ QtGui.QWidget.__init__(self, button)
+
+ self._isDraging = False
+ self._releaseOnClick = releaseOnClick
+
+ self.button = button
+ self.cursor = None
+
+ self.setCursor(cursor)
+ self.button.installEventFilter(self)
+ button.connect(button, QtCore.SIGNAL('clicked()'), self.startDrag)
+
+
+ def getReleaseOnClick(self):
+ return self._releaseOnClick
+
+ def setReleaseOnClick(self, flag):
+ self._releaseOnClick = flag
+
+
+ def isDraging(self):
+ """Checks if the drag tool is currently about to be draged"""
+ return self._isDraging
+
+
+ def startDrag(self):
+ """Starts dragging the cursor around"""
+
+ if self.cursor is None:
+ raise ValueError('No cursor set')
+
+ if not self.isDraging():
+ self._isDraging = True
+ #self.button.grabKeyboard()
+ self.button.grabMouse(self.cursor)
+ self.button.setMouseTracking(True)
+ pt = QtGui.QCursor().pos()
+ self.emit(QtCore.SIGNAL('startDrag(int, int)'), pt.x(), pt.y())
+
+
+ def getCursor(self):
+ """Returns the dragg cursor
+ @return: QCursor
+ """
+ return self.cursor
+
+
+ def setCursor(self, cursor):
+ """Sets the pixmap to be dragged around
+ @arg cursor: QCursor
+ """
+ self.cursor = cursor
+
+
+ def setGrabCursor(self, flag):
+ self.grab_cursor = flag
+
+ def getGrabCursor(self, flag):
+ return self.grab_cursor
+
+
+ def endDrag(self, x=0, y=0, canceled=False):
+ if self.isDraging():
+ self._isDraging = False
+ #self.button.releaseKeyboard()
+ self.button.releaseMouse()
+ self.button.setMouseTracking(False)
+ if canceled:
+ self.emit(QtCore.SIGNAL('dragCanceled()'))
+ else:
+ self.emit(QtCore.SIGNAL('draged(int, int)'), x, y)
+ self.emit(QtCore.SIGNAL('dragEnd()'))
+
+
+ def eventFilter(self, obj, event):
+
+ # TODO: grabKeyboard() will eat all keyboard input for a Gui but events never arrive here
+ #if event.type() == event.KeyRelease:
+ # if event.key() == QtCore.Qt.Key_Escape:
+ # self.endDrag(canceled=True)
+ # return True
+
+ if event.type() == event.MouseButtonPress:
+ if event.button()== QtCore.Qt.LeftButton:
+ if self.isDraging():
+ if self._releaseOnClick:
+ self.endDrag(event.globalX(), event.globalY(), canceled=False)
+ return True
+
+ elif event.type() == event.MouseButtonRelease:
+ if event.button()== QtCore.Qt.LeftButton:
+ if self.isDraging():
+ if not self._releaseOnClick:
+ self.emit(QtCore.SIGNAL('draged(int, int)'), event.globalX(), event.globalY())
+ return True
+ elif event.button()== QtCore.Qt.RightButton:
+ if self.isDraging():
+ self.endDrag(canceled=True)
+ return True
+
+ elif event.type() == event.MouseMove:
+ if self._isDraging:
+ try:
+ self.emit(
+ QtCore.SIGNAL('draging(int, int)'),
+ event.globalPos().x(),
+ event.globalPos().y()
+ )
+ except Exception, d: # just in case
+ self.endDrag(canceled=True)
+ return True
+
+ return False
+
+#******************************************************************************
+#
+#******************************************************************************
+if __name__ == '__main__':
+ import sys
+
+ # test tool. Click on the green square to take it up, anpther click
+ # will release it again
+ class TestTool(DragTool):
+
+ def __init__(self, parent, px, releaseOnClick=True):
+ DragTool.__init__(self, parent, px, releaseOnClick=releaseOnClick)
+ self.connect(
+ self,
+ QtCore.SIGNAL('draging(int, int)'),
+ self.on_draging
+ )
+ self.connect(
+ self,
+ QtCore.SIGNAL('draged(int, int)'),
+ self.on_draged
+ )
+
+ def on_draging(self, x, y):
+ print "moving: %s, %s" % (x, y)
+
+ def on_draged(self, x, y):
+ print "dragged: %s, %s" % (x, y)
+
+
+
+ app = QtGui.QApplication(sys.argv)
+
+ w = QtGui.QWidget(None)
+ w.setGeometry(50, 50, 200, 200)
+
+ # setup pixmap
+ px = QtGui.QPixmap(QtCore.QSize(32, 32))
+ px.fill(QtCore.Qt.green)
+
+ # setup dragg tool and button
+ dragTool = TestTool(
+ w,
+ QtGui.QCursor(px, 0, 0),
+ #releaseOnClick=False
+ )
+ dragTool.setGeometry(-1, -1, 1, 12)
+
+ bt = QtGui.QToolButton(w)
+ bt.setIcon(QtGui.QIcon(px))
+ w.connect(
+ bt,
+ QtCore.SIGNAL('clicked()'),
+ dragTool.startDrag
+ )
+
+
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
+
+
Added: trunk/fclient/fclient_lib/qt4ex/ctrls/editboxwrap.py
===================================================================
--- trunk/fclient/fclient_lib/qt4ex/ctrls/editboxwrap.py (rev 0)
+++ trunk/fclient/fclient_lib/qt4ex/ctrls/editboxwrap.py 2007-11-05 12:50:42 UTC (rev 47)
@@ -0,0 +1,201 @@
+"""Wrappers and tools for QLineEdit and QTextEdit"""
+
+from PyQt4 import QtCore, QtGui
+#***********************************************************************
+#
+#***********************************************************************
+class EditBoxContextMenuWrap(QtCore.QObject):
+ """Wrapper class for to handle custom context menus for editboxes
+
+ @note: make shure to always keep a reference to the wrapper
+ """
+
+ def __init__(self, edit):
+ """
+ @param edit: QTextEdit or QLineEdit
+ @attr edit: editbox
+ @attr actions: dict containing default actions for the editbox. Available actions are:
+ "Copy", "Cut", "Paste", "Redo", "SelectAll", "Undo". Caution: the dict is considered
+ to be read only!
+ @signal: 'customizeContextMenu(QObject* EditContextMenuWrap, QMenu* contextMenu)'.
+ This signal is emitted when the context menu for the editbox is requested. Use this signal
+ to customize the menu.
+ @signal: 'contextMenuActionSelected(QAction* action, bool isStandardAction)'.
+ Emitted when the user selected an action from the context menu. The flag indicates wether
+ a standard action or a custom action was selected
+
+ """
+ QtCore.QObject.__init__(self, edit)
+
+ self.edit = edit
+ self.actions = {}
+
+ edit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
+ self.connect(
+ edit,
+ QtCore.SIGNAL('customContextMenuRequested(const QPoint &)'),
+ self.__call__
+ )
+
+ def __call__(self, pt):
+
+ # create custom LineEdit context menu
+ self.actions = {}
+ m = QtGui.QMenu(self.edit)
+
+ # add default QLineEdit actions
+ actions = (
+ (
+ 'Undo',
+ self.edit.trUtf8("&Undo"),
+ QtGui.QKeySequence.Undo,
+ self.trUtf8('Undos the last ation'),
+ self.edit.undo,
+ ),
+ (
+ 'Redo',
+ self.edit.trUtf8("&Redo"),
+ QtGui.QKeySequence.Redo,
+ self.trUtf8('Redos the last ation'),
+ self.edit.redo,
+ ),
+ (
+ 'Cut',
+ self.edit.trUtf8("Cu&t"),
+ QtGui.QKeySequence.Cut,
+ self.trUtf8('Cuts the currently selected text'),
+ self.edit.cut,
+ ),
+ (
+ 'Copy',
+ self.edit.trUtf8("&Copy"),
+ QtGui.QKeySequence.Copy,
+ self.trUtf8('Copies the currently selected text'),
+ self.edit.copy,
+ ),
+ (
+ 'Paste',
+ self.edit.trUtf8("&Paste"),
+ QtGui.QKeySequence.Paste,
+ self.trUtf8('Pastes text from the clipboard'),
+ self.edit.paste,
+ ),
+ (
+ 'SelectAll',
+ self.edit.trUtf8("Select All"),
+ QtGui.QKeySequence.SelectAll,
+ self.trUtf8('Selects all text'),
+ self.edit.selectAll,
+ ),
+ #TODO: Delete
+ #d->actions[QLineEditPrivate::ClearAct] = new QAction(QLineEdit::tr("Delete"), this);
+ #QObject::connect(d->actions[QLineEditPrivate::ClearAct], SIGNAL(triggered()), this, SLOT(_q_deleteSelected()));
+ )
+
+ # setup actions
+ for name, text, shortcut, tip, trigger in actions:
+ act = QtGui.QAction(text, None)
+ act.setObjectName(name)
+ act.setStatusTip(tip)
+ if shortcut.__class__.__name__ == 'StandardKey':
+ # BUG: in PyQt4.3 - already reported
+ # initializing QKeySequence() with a Qt.StandardKey gives segfault
+ act.setShortcut(shortcut)
+ else:
+ act.setShortcut(QtGui.QKeySequence(shortcut) )
+ self.connect(act, QtCore.SIGNAL('triggered()'), trigger)
+ m.addAction(act)
+ self.actions[name] = act
+
+ # adjust actions
+ # NOTE: disabling "SelectAll" is somewhat unnecessary, so leave it out
+ if isinstance(self.edit, QtGui.QLineEdit):
+ canPaste = not QtGui.QApplication.clipboard().text().isEmpty()
+ canRedo = self.edit.isRedoAvailable()
+ canUndo = self.edit.isUndoAvailable()
+ hasSelection = self.edit.hasSelectedText()
+ isReadOnly = self.edit.isReadOnly()
+ else:
+ canPaste = self.edit.canPaste()
+ canRedo = self.edit.document().isRedoAvailable()
+ canUndo = self.edit.document().isUndoAvailable()
+ hasSelection = self.edit.textCursor().hasSelection()
+ isReadOnly = self.edit.isReadOnly()
+
+ self.actions['Undo'].setEnabled(canUndo)
+ self.actions['Redo'].setEnabled(canRedo)
+ self.actions['Cut'].setEnabled(not isReadOnly and hasSelection)
+ self.actions['Copy'].setEnabled(hasSelection)
+ self.actions['Paste'].setEnabled(not isReadOnly and canPaste)
+ self.actions['SelectAll'].setEnabled(True)
+
+ # TODO: control chars submenu not yet implemented
+ # one problem is that one can not remove a menu from a menu
+ #~ ControlCharacters = (
+ #~ (QT_TRANSLATE_NOOP("QUnicodeControlCharacterMenu", "LRM Left-to-right mark"), 0x200e),
+ #~ (QT_TRANSLATE_NOOP("QUnicodeControlCharacterMenu", "RLM Right-to-left mark"), 0x200f),
+ #~ (QT_TRANSLATE_NOOP("QUnicodeControlCharacterMenu", "ZWJ Zero width joiner"), 0x200d),
+ #~ (QT_TRANSLATE_NOOP("QUnicodeControlCharacterMenu", "ZWNJ Zero width non-joiner"), 0x200c),
+ ...
[truncated message content] |