SF.net SVN: fclient: [604] trunk/fclient/src/fclient/lib
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-07-13 09:42:55
|
Revision: 604
http://fclient.svn.sourceforge.net/fclient/?rev=604&view=rev
Author: jUrner
Date: 2008-07-13 02:43:02 -0700 (Sun, 13 Jul 2008)
Log Message:
-----------
reorganisation of qt4ex
Added Paths:
-----------
trunk/fclient/src/fclient/lib/qt4ex/
trunk/fclient/src/fclient/lib/qt4ex/LICENCE.MIT
trunk/fclient/src/fclient/lib/qt4ex/README
trunk/fclient/src/fclient/lib/qt4ex/__init__.py
trunk/fclient/src/fclient/lib/qt4ex/areatips.py
trunk/fclient/src/fclient/lib/qt4ex/checkarraywrap.py
trunk/fclient/src/fclient/lib/qt4ex/colorbutton.py
trunk/fclient/src/fclient/lib/qt4ex/dlgabout.py
trunk/fclient/src/fclient/lib/qt4ex/dlgfindreplace.py
trunk/fclient/src/fclient/lib/qt4ex/dlgpreferences.py
trunk/fclient/src/fclient/lib/qt4ex/dragtool.py
trunk/fclient/src/fclient/lib/qt4ex/editboxwrap.py
trunk/fclient/src/fclient/lib/qt4ex/labelwrap.py
trunk/fclient/src/fclient/lib/qt4ex/lang/
trunk/fclient/src/fclient/lib/qt4ex/lang/qt4ex_de.ts
trunk/fclient/src/fclient/lib/qt4ex/lang/qt4ex_en.ts
trunk/fclient/src/fclient/lib/qt4ex/lib/
trunk/fclient/src/fclient/lib/qt4ex/lib/__init__.py
trunk/fclient/src/fclient/lib/qt4ex/lib/assistant.py
trunk/fclient/src/fclient/lib/qt4ex/lib/language.py
trunk/fclient/src/fclient/lib/qt4ex/lib/resources.py
trunk/fclient/src/fclient/lib/qt4ex/lib/settings.py
trunk/fclient/src/fclient/lib/qt4ex/lib/tools.py
trunk/fclient/src/fclient/lib/qt4ex/mrumenu.py
trunk/fclient/src/fclient/lib/qt4ex/res/
trunk/fclient/src/fclient/lib/qt4ex/scripts/
trunk/fclient/src/fclient/lib/qt4ex/scripts/__init__.py
trunk/fclient/src/fclient/lib/qt4ex/scripts/manifest.py
trunk/fclient/src/fclient/lib/qt4ex/scripts/pylupdate.py
trunk/fclient/src/fclient/lib/qt4ex/scripts/qtpro.py
trunk/fclient/src/fclient/lib/qt4ex/tablewidget.py
trunk/fclient/src/fclient/lib/qt4ex/toolbarwrap.py
trunk/fclient/src/fclient/lib/qt4ex/tpls/
trunk/fclient/src/fclient/lib/qt4ex/tpls/DlgAbout.ui
trunk/fclient/src/fclient/lib/qt4ex/tpls/DlgFindReplace.ui
trunk/fclient/src/fclient/lib/qt4ex/tpls/DlgPreferencesTree.ui
trunk/fclient/src/fclient/lib/qt4ex/tpls/Ui_DlgAbout.py
trunk/fclient/src/fclient/lib/qt4ex/tpls/Ui_DlgFindReplace.py
trunk/fclient/src/fclient/lib/qt4ex/tpls/Ui_DlgPreferencesTree.py
trunk/fclient/src/fclient/lib/qt4ex/tpls/__init__.py
trunk/fclient/src/fclient/lib/qt4ex/treewidgetwrap.py
Added: trunk/fclient/src/fclient/lib/qt4ex/LICENCE.MIT
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/LICENCE.MIT (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/LICENCE.MIT 2008-07-13 09:43:02 UTC (rev 604)
@@ -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/src/fclient/lib/qt4ex/README
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/README (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/README 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,30 @@
+ qt4ex -- Qt5 / PyQt4 extensions
+
+
+
+Version history:
+
+
+*******************************************************************
+0.2.0
+*******************************************************************
+complete reorganisation of the package. too many changes to list here
+
+
+*******************************************************************
+0.1.0
+*******************************************************************
+news:
+ x. initial release
+
+bugfixes:
+ x.
+
+
+
+
+
+
+
+
+
Added: trunk/fclient/src/fclient/lib/qt4ex/__init__.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/__init__.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/__init__.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,11 @@
+"""Qt4 extensions
+
+"""
+
+__version__ = '0.2.0'
+__author__ = 'Juergen Urner'
+__emeil__ = 'jU...@ar...'
+__licence__ = 'Mit'
+__copyright__ = '(c) 2007-2008 Juergen Urner'
+
+
Added: trunk/fclient/src/fclient/lib/qt4ex/areatips.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/areatips.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/areatips.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,463 @@
+# -*- 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
+#***************************************************************************
+#
+#***************************************************************************
+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/src/fclient/lib/qt4ex/checkarraywrap.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/checkarraywrap.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/checkarraywrap.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -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/src/fclient/lib/qt4ex/colorbutton.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/colorbutton.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/colorbutton.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,228 @@
+"""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
+"""
+from __future__ import absolute_import
+if __name__ == '__main__': # see --> http://bugs.python.org/issue1510172 . works only current dir and below
+ import os; __path__ = [os.path.dirname(__file__)]
+
+
+import sys, os
+from PyQt4 import QtCore, QtGui
+
+from .lib import tools
+#************************************************************************
+#
+#************************************************************************
+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 Qt4ExTools.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/src/fclient/lib/qt4ex/dlgabout.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/dlgabout.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/dlgabout.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,123 @@
+from __future__ import absolute_import
+if __name__ == '__main__': # see --> http://bugs.python.org/issue1510172 . works only current dir and below
+ import os; __path__ = [os.path.dirname(__file__)]
+
+import sys, os
+import cPickle
+from PyQt4 import QtCore, QtGui
+
+from .tpls.Ui_DlgAbout import Ui_DlgAbout
+from .lib import tools
+#*********************************************************************
+#
+#*********************************************************************
+class DlgAbout(QtGui.QDialog, Ui_DlgAbout):
+
+ # control ids
+ IdLabelAbout = 'label_about'
+
+
+ def __init__(self,
+ parent,
+ state=None,
+ caption=None,
+ appName=None,
+ description=None,
+ version=None,
+ author=None,
+ email=None,
+ licence=None,
+ copyright=None,
+ ):
+ """
+ @param state: formerly saved state of the dialog or None
+ @param caption: ttle of the dialog or None
+ @note: all other keyword params should be pretty self explanatory and
+ should be (str, QString).
+
+ """
+
+ QtGui.QDialog.__init__(self, parent)
+ self.setupUi(self)
+
+ if caption is not None:
+ self.setWindowTitle(caption)
+
+ label = self.controlById(self.IdLabelAbout)
+
+ text = ''
+ if appName is not None:
+ text += '<h3>%s</h3>\n' % appName
+ if description is not None:
+ text += '<hr>%s<hr>\n' % description
+
+ text += '<ul>\n'
+ if version is not None:
+ text += ' <li>Version: %s\n' % version
+ if author is not None:
+ text += ' <li>Author: %s\n' % author
+ if email is not None:
+ text += ' <li>Email: %s\n' % email
+ if licence is not None:
+ text += ' <li>Licence: %s\n' % licence
+ if copyright is not None:
+ text += ' <li>Copyright: %s\n' % copyright
+
+ for name, version in tools.versionInfo():
+ text += ' <li>%s: %s\n' % (name, version)
+
+ text += '</ul>\n'
+ label.setText(text)
+
+ # restore state
+ if state is not None:
+ self.restoreState(state)
+
+
+ def controlById(self, id_control):
+ """Returns a widget of the dialog given its id"""
+ return getattr(self, id_control)
+
+
+ def saveState(self):
+ """Saves the state of the dialog
+ @return: (str) state
+ """
+ return cPickle.dumps(self.saveGeometry(), 0)
+
+
+ def restoreState(self, state):
+ """Restores the state of the dialog
+ @param state: formerly saved state
+ @return True if successful, False otherwise
+ """
+ result = False
+ try:
+ state = cPickle.loads(state)
+ except: pass
+ else:
+ try:
+ result = self.restoreGeometry(state)
+ except: pass
+ return result
+
+#***********************************************************************
+#
+#***********************************************************************
+if __name__ == '__main__':
+ import sys
+
+ app = QtGui.QApplication(sys.argv)
+ w = DlgAbout(None,
+ caption=None,
+ appName=None,
+ description=None,
+ version=None,
+ author=None,
+ email=None,
+ licence=None,
+ copyright=None,
+ )
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
Added: trunk/fclient/src/fclient/lib/qt4ex/dlgfindreplace.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/dlgfindreplace.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/dlgfindreplace.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,400 @@
+
+# TODO: feedback on how many occurences have been replaced
+from __future__ import absolute_import
+if __name__ == '__main__': # see --> http://bugs.python.org/issue1510172 . works only current dir and below
+ import os; __path__ = [os.path.dirname(__file__)]
+
+import os, sys
+import cPickle
+
+from PyQt4 import QtCore, QtGui
+
+from .tpls.Ui_DlgFindReplace import Ui_DlgFindReplace
+from . import checkarraywrap
+#*****************************************************************
+# find parameters
+#*****************************************************************
+class FindParams(QtCore.QObject):
+ """data class that holds parameters of the current search"""
+
+ DlgReplace = 0x1 # displays a replace dialog
+
+ ActionDownwards = 0x100 # search direction is downwards
+ ActionUpwnwards = 0x200 # search direction is upwards
+ ActionReplace = 0x400 # replace
+ ActionReplaceAll = 0x800 # replace all
+ MaskActionsFind = ActionDownwards | ActionUpwnwards
+ MaskActionsReplace = ActionReplace | ActionReplaceAll
+ MaskActions = MaskActionsFind | MaskActionsReplace
+
+ MatchNone = 0
+ MatchWholeWord = 0x10000 # searches whole word
+ MatchCase = 0x20000 # searches case sensitive
+ MatchRegularExpression = 0x40000 # regular expression
+ MatchCustom1 = 0x80000
+ MatchCustom2 = 0x100000
+ MatchCustom3 = 0x200000
+ MaskMatches = MatchWholeWord | MatchCase | MatchRegularExpression | \
+ MatchCustom1 | MatchCustom1 | MatchCustom1
+
+
+ __slots__ = ("flags", "find", "replace")
+ def __init__(self, parent):
+ """constructor
+
+ @param parent: parent
+ @attr flags: one or more Action*, Dlg* and Match* flags
+ @attr find: (QString) string to find
+ @attr replace: (QString) string to replace
+
+ @cvar DlgReplace: displays a Replace dialog
+
+ @cvar ActionDownwards: the user hit the "Find Next" button
+ @cvar ActionUpwards: the user hit the "Upwards" button
+ @cvar ActionReplace: the user hit the "Replace" button
+ @cvar ActionReplaceAll: the user hit the "Replace All" button
+ @cvar MaskActionsFind: mask for find actions
+ @cvar MaskActionsReplace: mask for replace actions
+ @cvar MaskActions: mask for actions
+
+ @cvar MatchNone: default match flag
+ @cvar MatchWholeWord: the user checked the "Match Word" checkbox
+ @cvar MatchCase: the user checked the "Match Case" checkbox
+ @cvar MatchRegualrExpressioin: the user checked the "Regualr Expression" checkbox
+ @cvar MatchCustom1: the user checked the custom-1 checkbox
+ @cvar MatchCustom2: the user checked the custom-2 checkbox
+ @cvar MatchCustom3: the user checked the custom-3 checkbox
+ @cvar MaskMatches: mask for match checkboxes
+
+ """
+ QtCore.QObject.__init__(self, parent)
+
+ self.flags = self.MatchNone
+ self.find = QtCore.QString()
+ self.replace = QtCore.QString()
+
+
+ def giveFeedback(self, n):
+ """Notifies listenrers about the result of a find or replace action
+ @param n: (uint) number of occurences found or replaced
+ """
+ self.emit(QtCore.SIGNAL('giveFeedback(int)'), n)
+
+
+ def printFindParams(self):
+ """helper for debugging, prints out all info gatherd in the class"""
+ print '-------------------------------------------'
+ print self.flags & self.DlgReplace and "<Replace Dialog>" or "<Find Dialog>"
+ print "Action:", {
+ self.ActionDownwards: "Find Downwards",
+ self.ActionUpwnwards: "Find Upwards",
+ self.ActionReplace: "Replace",
+ self.ActionReplaceAll: "Replace All",
+ }[self.flags & self.MaskActions]
+ print 'Find:', repr(str(self.find))
+ print 'Replace:', repr(str(self.replace))
+ print "Match Case:", bool(self.flags & self.MatchCase)
+ print "Match Word:", bool(self.flags & self.MatchWholeWord)
+ print "Match Regex:", bool(self.flags & self.MatchRegularExpression)
+ print "Match custom1:", bool(self.flags & self.MatchCustom1)
+ print "Match Custom2:", bool(self.flags & self.MatchCustom2)
+ print "Match Custom3:", bool(self.flags & self.MatchCustom3)
+ print '-------------------------------------------'
+
+#********************************************************************
+#
+#********************************************************************
+class DlgFindReplace(QtGui.QDialog, Ui_DlgFindReplace):
+ """Displays a find or replace dialog
+
+ In addition to the default checks for MatchWord, MatchCase, MatchRegularExperession
+ the dialog supports up to three custom checkboxes.
+
+
+ sample code:
+
+
+ def onReplaceWhatever(self):
+
+ # setup find params
+ findParams = FindParams(self)
+
+ # display a replace dialog
+ findParams.flags = FindParams.DlgReplace
+
+ # checks to set initially
+ findParams.flags |= FindParams.WholeWord | FindParams.MatchCase
+
+ # adjust checks to be shown
+ showChecks = FindParams.WholeWord | FindParams.MatchCase | FindParams.Custom1
+
+ # create dialog
+ dlg = DlgFindReplace(parent
+ #state=self.getDialogFindReplaceLastSate(),
+ findParams=findParams,
+ showChecks=showChecks,
+ customChecks=('My Custom Checkbox', ),
+ # if find or replace history was saved last time the dialog was run, pass them
+ #findHistory=MySavedFindHistory,
+ #replaceHistory=MySavedReplaceHistory,
+ )
+
+ # connect and run dialog
+ self.connect(dlg, QtCore.SIGNAL('find(QObject*)'), self.onReplaceDialog)
+ dlg.exec_()
+
+ # save state if desired
+ state = dlg.saveState()
+
+ def onReplaceDialog(self, findParams):
+
+ if findParams.flags & FindParams.ReplaceAll:
+
+ # user checked 'My Custom Checkbox'
+ myFlag = bool(findParams.flags & FindParams.MatchCustom1)
+
+ # do replacement
+ nReplacements = self.replaceWhatever(myFlag)
+
+ # notify dialog about the changes
+ findParams.notifyChanges(nReplacements)
+ """
+
+ # consts
+ IdLabelFind = 'labelFind'
+ IdLabelReplace = 'labelReplace'
+ IdCmbFind = 'cmbFind'
+ IdCmbReplace = 'cmbReplace'
+
+ IdBtActionDownwards = 'btFindDownwards'
+ IdBtActionUpwnwards = 'btFindUpwards'
+ IdBtReplace = 'btReplace'
+ IdBtReplaceAll = 'btReplaceAll'
+ IdBtOk = 'btOk'
+
+ IdLabelStatus = 'labelStatus'
+
+ IdCkWholeWord = 'ckWholeWord'
+ IdCkMatchCase = 'ckMatchCase'
+ IdCkRegularExpression = 'ckRegularExpression'
+ IdCkCustom1 = 'ckCustom1'
+ IdCkCustom2 = 'ckCustom2'
+ IdCkCustom3 = 'ckCustom3'
+
+ Checks = {
+ FindParams.MatchWholeWord: IdCkWholeWord,
+ FindParams.MatchCase: IdCkMatchCase,
+ FindParams.MatchRegularExpression: IdCkRegularExpression,
+ FindParams.MatchCustom1: IdCkCustom1,
+ FindParams.MatchCustom2: IdCkCustom2,
+ FindParams.MatchCustom3: IdCkCustom3,
+ }
+ CustomChecks = (IdCkCustom1, IdCkCustom2, IdCkCustom3)
+
+
+ def __init__(self, parent,
+ state=None,
+ findParams=None,
+ showChecks=None,
+ customChecks=None,
+ ):
+ """
+ @param findParams: FindParams instance. The state of the checkboxes is initialized according
+ to FindParams.flags (Match* flags) in the FindParams instance passed. If the DlgReplace flag is
+ set, a Replace dialog is displayed.
+ @param state: (str) state of the dialog to restore or None (overwrites initial findParams)
+ @param showChecks: FindParams.Match* flags with checks to show. Default shows all checks
+ @param customChecks: (list QStrings) text for custom checkboxes or None
+
+ @signal SIGNAL('find(QObject* findParams)'): emitted when the user hits one of the find or replace buttons.
+ """
+
+ QtGui.QDialog.__init__(self, parent)
+ self.setupUi(self)
+
+ self._checkArray = None
+ self._findParams = findParams
+
+ # init buttons
+ buttons = (
+ (self.IdBtActionDownwards, self.onBtActionDownwards),
+ (self.IdBtActionUpwnwards, self.onBtActionUpwnwards),
+ (self.IdBtReplace, self.onBtReplace),
+ (self.IdBtReplaceAll, self.onBtReplaceAll),
+ (self.IdBtOk, self.close),
+ )
+ for idControl, cb in buttons:
+ self.connect(self.controlById(idControl), QtCore.SIGNAL('clicked()'), cb)
+
+ if findParams is not None:
+
+ self.connect(findParams, QtCore.SIGNAL('giveFeedback(int)'), self.onGiveFeedback)
+
+ # init find or replace
+ if findParams.flags & findParams.DlgReplace:
+ self.setWindowTitle(self.trUtf8("Replace..."))
+ else:
+ self.setWindowTitle(self.trUtf8("Find..."))
+ for idControl in (self.IdLabelReplace, self.IdCmbReplace, self.IdBtReplace, self.IdBtReplaceAll):
+ ctrl = self.controlById(idControl)
+ ctrl.setEnabled(False)
+ ctrl.setVisible(False)
+
+
+ # init checks
+ checks = self.Checks.copy()
+ for const, idControl in checks.items():
+ checks[const] = self.controlById(idControl)
+ self._checkArray = checkarraywrap.CheckArrayWrap(
+ self,
+ checks,
+ value=findParams.flags
+ )
+ if findParams is not None:
+ self._checkArray.setChecks(findParams.flags & findParams.MaskMatches)
+
+ if showChecks is not None:
+ self._checkArray.showChecks(showChecks)
+ if customChecks is not None:
+ customChecks = list(customChecks)
+ for n, idControl in enumerate(self.CustomChecks):
+ ck = self.controlById(idControl)
+ if customChecks:
+ ck.setText(customChecks.pop(0))
+ else:
+ ck.setEnabled(False)
+ ck.setVisible(False)
+ if customChecks:
+ raise ValueError('Only %s custom checks supported' % len(CustomChecks))
+
+ # TODO: check if overwrite is ok
+ if state is not None:
+ self.restoreState(state)
+
+ ############################################
+ ## events
+ ############################################
+ def showEvent(self, event):
+ label = self.controlById(self.IdLabelStatus)
+ label.setText(self.trUtf8('Ready'))
+ self.resize(self.width(), 0)
+ self.setFixedHeight(self.height())
+
+ ################################################
+ ## slots
+ ################################################
+ def onBtActionDownwards(self): self._emitSignalFind(FindParams.ActionDownwards)
+ def onBtActionUpwnwards(self): self._emitSignalFind(FindParams.ActionUpwnwards)
+ def onBtReplace(self): self._emitSignalFind(FindParams.ActionReplace)
+ def onBtReplaceAll(self): self._emitSignalFind(FindParams.ActionReplaceAll)
+
+ def onGiveFeedback(self, n):
+ """Slot called when the client gives feedback via FindParams()"""
+ label = self.controlById(self.IdLabelStatus)
+ if self._findParams.flags & FindParams.MaskActionsReplace:
+ label.setText(self.trUtf8('Replaced:') + ' ' + str(n))
+ else:
+ label.setText(self.trUtf8('Found:') + ' ' + str(n))
+
+ ##############################################
+ ## private methods
+ ##############################################
+ def _emitSignalFind(self, flagFind):
+ """Emits the find signal"""
+ if self._findParams is None:
+ return
+
+ flags = self._checkArray.getValue()
+ flags &= ~self._findParams.MaskActions
+ flags |= flagFind
+ self._findParams.flags = flags
+ self._findParams.find = self.controlById(self.IdCmbFind).lineEdit().text()
+ if flags & self._findParams.DlgReplace:
+ self._findParams.replace = self.controlById(self.IdCmbReplace).lineEdit().text()
+ else:
+ self._findParams.replace = ""
+ self.emit(QtCore.SIGNAL('find(QObject*)'), (self._findParams))
+
+ ############################################
+ ## methods
+ ############################################
+ def controlById(self, id_control):
+ """Returns a control fiven its id (one of the Id* consts as defined by the module)"""
+ return getattr(self, id_control)
+
+
+ def saveComboHistory(self, idCmb):
+ """Saves the find or replace history
+ @param idCmb: id of the combobox to save the history for
+ @return (QStringList) history
+ """
+ cmb = self.controlById(IdCmb)
+ L = QtCore.QStringList()
+ for i in range(cmb.count()):
+ L << cmb.itemText(i)
+ return L
+
+
+ def saveState(self):
+ """Saves the state of the dialog
+ @return: (str) state
+ """
+ state = {
+ 'checks': self._findParams.flags & FindParams.MaskMatch,
+ 'geometry': self.saveGeometry(),
+ 'historyFind': self.saveComboHistory(self.IdCmbFind),
+ 'historyReplace': self.saveComboHistory(self.IdCmbReplace),
+ }
+ return cPickle.dumps(state, 0)
+
+
+ def restoreState(self, state):
+ """Restors the state of the dialog
+ @param state: (str) state to restore
+ """
+ # TODO: not tested
+ result = False
+ try:
+ state = cPickle.loads(state)
+ except Exception, d: pass
+ else:
+ #
+ try:
+ self.restoreGeometry(tate.get('geometry', None))
+ except Exception, d: pass
+ # restore combos
+ try:
+ self.controlById(self.IdCmbFind).addItems(state.get('historyFind', None))
+ except Exception, d: pass
+ try:
+ self.controlById(self.IdCmbReplace).addItems(state.get('historyReplace', None))
+ except Exception, d: pass
+ # restore checks
+ try:
+ self._checkArray.setChecks(state.get('checks', 0))
+ except Exception, d: pass
+
+
+#***********************************************************************
+#
+#***********************************************************************
+if __name__ == '__main__':
+ import sys
+
+ app = QtGui.QApplication(sys.argv)
+
+ findParams = FindParams(None)
+ findParams.flags |= findParams.DlgReplace
+ w =DlgFindReplace(None,
+ findParams=findParams,
+ customChecks=('A Custom Checkbox', ),
+ )
+
+ def cb(findParams):
+ findParams.printFindParams()
+ findParams.giveFeedback(10)
+ w.connect(w, QtCore.SIGNAL('find(QObject*)'), cb)
+
+ w.show()
+ res = app.exec_()
+ sys.exit(res)
Added: trunk/fclient/src/fclient/lib/qt4ex/dlgpreferences.py
===================================================================
--- trunk/fclient/src/fclient/lib/qt4ex/dlgpreferences.py (rev 0)
+++ trunk/fclient/src/fclient/lib/qt4ex/dlgpreferences.py 2008-07-13 09:43:02 UTC (rev 604)
@@ -0,0 +1,821 @@
+"""Preferences dialog"""
+from __future__ import absolute_import
+if __name__ == '__main__': # see --> http://bugs.python.org/issue1510172 . works only current dir and below
+ import os; __path__ = [os.path.dirname(__file__)]
+
+# TODO:
+#
+# x. how to set some reasonable initial size? Mabe split the page population
+# process into two stages: 1. precreate all page widgets 2. tell the individual page
+# to init settings
+#
+# widget = page.create(parent)
+# page.initialize()
+# page.setVisible(flag)
+#
+
+
+
+import sys, os
+import cPickle
+
+from PyQt4 import QtCore, QtGui
+
+from .tpls.Ui_DlgPreferencesTree import Ui_DlgPreferencesTree
+from .treewidgetwrap import TreeWidgetIterator
+#*********************************************************************
+#
+#*********************************************************************
+class Page(QtCore.QObject):
+ """Base class for preferences dialog pages
+
+ usage:
+
+ overwrite setVisible() to create the widget ontaining widgets of the preferences page
+ (if not already done) in the call. Overwrite all the can*() and do*() methods. Call
+ setDirty(True) when appropriate.
+
+ sample code:
+
+ class MyPage(Page):
+
+ def __init__(self, *args):
+ Page.__init__(self, *args)
+ self._widget = None
+
+ def setVisible(self, parent, flag):
+ createdNew = False
+ if flag and self._widget is None:
+ self._widget = self.CreateMyWidget(parent)
+ createdNew = True
+ self._widget.setVisible(flag)
+ return (createdNew, self._widget)
+
+ ...use __call__() to setup hirarchical trees of pages
+
+ page = Page('Parent')(
+ Page('ChildPage')(
+ Page('ChildPage')
+ )
+ )
+
+
+ """
+
+ _version_ = 1 # version number
+
+ def __init__(self, uuid):
+ """Creates a new preferences page
+ @param uuid: (str) unique id of the page.
+ @note: currrently no check is done to eshure the uuid is unique throughout the tree of pages.
+ """
+ QtCore.QObject.__init__(self)
+ self._pageData = { # internal state data
+ 'children': [],
+ 'isDirty': False,
+ 'lastSelectedChildPage': None,
+ 'parent': None,
+ 'userData': None,
+ 'uuid': uuid,
+ }
+
+ def __call__(self, *others):
+ """Adds one or more pages as child pages to a page"""
+ children = self._pageData['children']
+ for i in others:
+ children.append(i)
+ i._pageData['parent'] = self
+ return self
+
+ def canApply(self):
+ """Should return a flag indicating if changes can be emidiatey applied by the page"""
+ return False
+
+ def canHelp(self):
+ """Should return a flag indicating if a help page is available for the page"""
+ return False
+
+ def canRestoreDefaults(self):
+ """Should return a flag indicating if the page can rstore default value"""
+ return False
+
+ def children(self):
+ """Returns a list containing all emidiate child pages of the page"""
+ return self._pageData['children']
+
+ def displayName(self):
+ """Should return the name of the page as it is to be displayed by the preferences dialog
+ @return: QString
+ """
+ return self.trUtf8('Page')
+
+ def doApply(self):
+ """Should apply changes made
+ @return: True on success, False otherisde
+ """
+ return True
+
+ def doCancel(self):
+ """Should cancel all changes made
+ @return: True on success, False otherisde
+ """
+ return True
+
+ def doOk(self):
+ """Should apply changes
+ @return: True on success, False otherisde
+ """
+ return True
+
+ def doRestoreDefaults(self):
+ """Should restore defaults
+ @return: True on success, False otherisde
+ """
+ return True
+
+ def doHelp(self):
+ """Should pop up a help page for the page"""
+
+ def icon(self):
+ """Should return the icon associated to a page or None"""
+
+ def isDirty(self):
+ """Should return a flag indicating if the page has unsaved changes"""
+ return self._pageData['isDirty']
+
+ def lastSelectedChildPage(self):
+ """Returns the uuid of the most recently selected child page of the current page"""
+ return self._pageData['lastSelectedChildPage']
+
+ def pageFromUuid(self, uuid):
+ """Traverses the page and all its children to return a page given its uuid
+ @return: page or None if no page was found"""
+ for page in self.walk():
+ if page.uuid() == uuid:
+ return page
+
+ def parent(self):
+ """Returns the parent page of the page"""
+ return self._pageData['parent']
+
+ def setDirty(self, flag):
+ """Sets or clears the dirty flag"""
+ self._pageData['isDirty'] = bool(flag)
+ self.emit(QtCore.SIGNAL('setDirty(bool)'), self._pageData['isDirty'])
+
+ def setUserData(self, data):
+ """Assignes any user data to the page
+ @param data: any data
+ @note: user data is reserved to Ui implementations displaying pages, do not use.
+ """
+ self._pageData['userData'] = data
+
+ def setLastSelectedChildPage(self, uuid):
+ """Stores the specified uuid as most recently selected child page of the current page"""
+ self._pageData['lastSelectedChildPage'] = uuid
+
+ def setVisible(self, parent, flag):
+ """Should show or hide the page or create it if not already done
+ @param parent: parent widget
+ @param flag: True if the page is to be shown, False if it is to be hidden
+ @return: tuple(bool createdNew, QWidget widget)
+ """
+ return (False, None)
+
+ def userData(self):
+ """Returns internal user data"""
+ return self._pageData['userData']
+
+ def uuid(self):
+ """Returns the uuid of the page"""
+ return self._pageData['uuid']
+
+ def walk(self, report=False):
+ """Walks over a page and all its children
+ @param report: if If False the next page in turn is returned. If True, a tupple(flag, page) for the next
+ page in turn is returned, where flag is 1 if a page is a container for child pages, 0 if a page has no
+ child pages and -1 if traversal of a pages child pages is completed.
+ """
+ if report:
+ def walker(page):
+ children = page.children()
+ if children:
+ yield 1, page
+ for childPage in children:
+ for x in walker(childPage):
+ yield x
+ yield -1, page
+ else:
+ yield 0, page
+ else:
+ def walker(page):
+ yield page
+ for childPage in page.children():
+ for x in walker(childPage):
+ yield x
+
+ return walker(self)
+
+#*********************************************************************
+#
+#*********************************************************************
+class PageControler(QtCore.QObject):
+ """Controler for pages"""
+
+ PageQueryParent = 1
+ PageAdd = 2
+
+ PrefixCapability = 'can'
+ PrefixCapabilityApply = 'do'
+ MethodNameIsDirty = 'isDirty'
+ MethodNameApply = 'doApply'
+
+
+ def __init__(self, ui, cb, pageContainer, buttonBox, maxPageDepth=None, pages=None):
+ """
+ @param ui: (QDialog) parent
+ @param cb: callback
+ @param pageContainer: (QWidget) displaying pages
+ @param maxPageDepth: (int) maximum nesting depth of pages. If None pages
+ may be nested up to sys.maxint
+ @param opages: tree of ppages to control
+
+ @note: the callback will be called with three arguments:
+
+ 1. one of the Page* consts to indicate the action
+ 2. message dependend param
+ 3. message dependend param
+
+ messages:
+
+ 1. PageQueryParent = the parent for all pages is requested
+ param1 = param2 = always none
+ return = (QWidget) parent
+ note = the parent returned will be passed
+ as param1 to all toplevel calls to PageAdd
+
+ 2. PageAdd = a page should be added.
+ param1 = return value of the last call to PageAdd the parent page or None
+ param2 = the page to be added
+ return = will be passed in the next call of PageAdd to all emidiate child pages
+
+ """
+ QtCore.QObject.__init__(self, ui)
+
+ self.buttonBox = buttonBox
+ self.cb = cb
+ self.grid = QtGui.QGridLayout(pageContainer)
+ self.lastSelectedPage = None
+ self.pageContainer = pageContainer
+ self.pages = pages
+ self.ui = ui
+
+ # buttons handld by the controler
+ self.buttonBoxButtons = {
+ QtGui.QDialogButtonBox.Apply: 'Apply',
+ QtGui.QDialogButtonBox.Cancel: 'Cancel',
+ QtGui.QDialogButtonBox.Help: 'Help',
+ QtGui.QDialogButtonBox.Ok: 'Ok',
+ QtGui.QDialogButtonBox.RestoreDefaults: 'RestoreDefaults',
+ }
+ # page depandend buttons of the button box
+ self.buttonBoxOptionalButtons = (
+ QtGui.QDialogButtonBox.Apply,
+ QtGui.QDialogButtonBox.Help,
+ QtGui.QDialogButtonBox.RestoreDefaults,
+ )
+
+
+ # dump sections and pages to tree
+ if pages is not None:
+
+ # drop root item
+ enum = iter(pages.walk(report=True))
+ flag, root = enum.next()
+
+ # dump items
+ parent = self.cb(self.PageQueryParent, None, None)
+ pageStack = [parent, ]
+ for flag, page in enum:
+ if flag == -1:
+ pageStack.pop()
+ continue
+ page.setParent(parent)
+ result = self.cb(self.PageAdd, pageStack[-1], page)
+
+ if flag == 1:
+ if maxPageDepth is not None:
+ if len(pageStack) > maxPageDepth:
+ raise ValueError('Pages nested to deeply (%s level allowed)' % maxPageDepth)
+ pageStack.append(result)
+
+ # setup button box
+ self.connect(
+ buttonBox,
+ QtCore.SIGNAL('clicked(QAbstractButton*)'),
+ self.onStandardButtonClicked
+ )
+ self.adjustStandardButtons(None)
+
+
+
+ ################################################
+ ## slots
+ ################################################
+ def onPageDirtyChanged(self, flag):
+ bt = self.buttonBox.button(QtGui.QDialogButtonBox.Apply)
+ if bt.isVisible():
+ bt.setEnabled(flag)
+
+
+ def onStandardButtonClicked(self, button):
+ if self.lastSelectedPage is None:
+ return
+
+ page = self.lastSelectedPage[0]
+ standardButton = self.buttonBox.standardButton(button)
+ standardButtonName = self.buttonBoxButtons.get(standardButton, None)
+ if standardButtonName is not None:
+ methodName = self.PrefixCapabilityApply + standardButtonName
+ method = getattr(page, methodName, None)
+ if method is None:
+ raise valueError('Pages must prvide a "%s" method' % methodName)
+ if method():
+ if standardButton == QtGui.QDialogButtonBox.Apply:
+ page.setDirty(False)
+
+
+ ##################################################
+ ## methods
+ ##################################################
+ def adjustStandardButtons(self, page):
+
+ for standardButton in self.buttonBoxOptionalButtons:
+ bt = self.buttonBox.button(standardButton)
+ if bt is None:
+ standardButtonName = self.buttonBoxButtons[standardButton]
+ raise ValueError('Button not found: ' + standardButtonName)
+
+ # show / hide standard buttons
+ flag = False
+ if page is not None:
+ standardButtonName = self.buttonBoxButtons[standardButton]
+ methodName = self.PrefixCapability + standardButtonName
+ method = getattr(page, methodName, None)
+ if method is None:
+ raise valueError('Pages must prvide a "%s" method' % methodName)
+ flag = method()
+ bt.setEnabled(flag)
+ bt.setVisible(flag)
+
+ # enable Apply button if necessary
+ if flag and standardButton == QtGui.QDialogButtonBox.Apply:
+ method = getattr(page, self.MethodNameIsDirty, None)
+ if method is None:
+ raise valueError('Pages must prvide a "%s" method' % self.MethodNameIsDirty)
+ bt.setEnabled(method())
+
+
+ def close(self):
+ """Close the controler
+ @note: make shure to call this method on exit to apply outstanding changes
+ """
+ if self.pages is not None:
+ for page in self.pages.walk():
+ if page.isDirty():
+ method = getattr(page, self.MethodNameApply, None)
+ if method is None:
+ raise ValueError('Pages must provide a "%s" method' % self.MethodNameApply)
+ method()
+
+
+
+ def currentPage(self):
+ """Returns the currently displayed page or None"""
+ if self.lastSelectedPage is not None:
+ return self.lastSelectedPage[0]
+
+
+ def currentPageUuid(self):
+ """Returns the uuid of the current page or None"""
+ page = self.currentPage()
+ if page is not None:
+ return page.uuid()
+
+
+ def setCurrentPage(self, page):
+ """Displays a page"""
+
+ # hide last page
+ if self.lastSelectedPage is not None:
+ oldPage, oldWidget = self.lastSelectedPage
+ if oldPage == page:
+ return
+ oldPage.setVisible(self.pageContainer, False)
+ self.grid.removeWidget(oldWidget)
+ self.disconnect(
+ page,
+ QtCore.SIGNAL('setDirty(bool)'),
+ self.onPageDirtyChanged
+ )
+ self.lastSelectedPage = None
+
+ # create next page if necessary
+ createdNew, widget = page.setVisible(self.pageContainer, True)
+ self.lastSelectedPage = (page, widget)
+
+ # setup page
+ self.adjustStandardButtons(page)
+ self.connect(
+ page,
+ QtCore.SIGNAL('setDirty(bool)'),
+ self.onPageDirtyChanged
+ )
+
+ # inform parent about changes
+ if page.parent() is not None:
+ page.parent().setLastSelectedChildPage(page.uuid())
+
+ # enshure enough space is available for the widget
+ if widget is not None:
+ if createdNew:
+ cx, cy, cw, ch = self.grid.getContentsMargins()
+ w = widget.width() + cx + cw
+ h = widget.height() + cy + ch
+ pageContainerSize = self.pageContainer.minimumSize()
+ pageContainerW = pageContainerSize.width()
+ pageContainerH = pageContainerSize.height()
+ if pageContainerW < w or pageContainerH < h:
+ if pageContainerW > w:
+ w = pageContainerW
+ if pageContainerH > h:
+ h = pageContainerH
+ self.pageContainer.setMinimumSize(w, h)
+ #
+ # add page widget
+ self.grid.addWidget(widget)
+ self.grid.activate()
+
+ ############################
+ ##widget.setAutoFillBackground(True)
+ ##pal = QtGui.QPalette()
+ ##pal.setColor(pal.Window, QtGui.QColor('white'))
+ ##widget.setPalette(pal)
+ #############################
+
+
+ def setCurrentPageFromUuid(self, uuid):
+ """Sets the current page from the pages uuid
+ @param uuid: unique id of the page
+ @return: the page selected or none
+ """
+ if self.pages is not None:
+ page = self.pages.pageFromUuid(uuid)
+ if page is not None:
+ self.setCurrentPage(page)
+ return page
+
+
+#*********************************************************************
+#
+#*********************************************************************
+class DlgPreferencesTree(QtGui.QDialog, Ui_DlgPreferencesTree):
+ """Preferences dialog that lets the user navigate a page hirarchy via a tree
+ """
+
+ IdButtonBox = 'buttonBox'
+ IdFramePages = 'framePages'
+ IdTreePages = 'treePages'
+ IdSplitter = 'splitter'
+
+ def __init__(self,
+ parent,
+ caption=None,
+ pages=None,
+ startPage=None,
+ state=None,
+ maxDepth=None,
+ ):
+ """
+ @param parent: parent window or ...
[truncated message content] |