| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # QuteCom, a voice over Internet phone |
|---|
| 4 | # Copyright (C) 2010 Mbdsys |
|---|
| 5 | # |
|---|
| 6 | # This program is free software you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation either version 2 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | # |
|---|
| 20 | # @author Aurelien Gateau |
|---|
| 21 | # @author Ovidiu Ciule |
|---|
| 22 | |
|---|
| 23 | from SafeConnect import * |
|---|
| 24 | from PyQt4.QtCore import * |
|---|
| 25 | from PyQt4.QtGui import * |
|---|
| 26 | import platform |
|---|
| 27 | |
|---|
| 28 | class DropDownWindow(QFrame): |
|---|
| 29 | """ |
|---|
| 30 | This is a pop-up window that appears under/above a button |
|---|
| 31 | Used for the smileys pop-up selection menu in the chat window, among other |
|---|
| 32 | uses |
|---|
| 33 | """ |
|---|
| 34 | def __init__(self, button): |
|---|
| 35 | self.__button = button |
|---|
| 36 | QFrame.__init__(self, button, Qt.Popup | Qt.FramelessWindowHint) |
|---|
| 37 | connect(button, SIGNAL("toggled(bool)"), self.buttonToggledSlot) |
|---|
| 38 | |
|---|
| 39 | def showEvent(self, event): |
|---|
| 40 | rect = QApplication.desktop().availableGeometry(self.__button) |
|---|
| 41 | windowWidth = self.sizeHint().width() |
|---|
| 42 | windowHeight = self.sizeHint().height() |
|---|
| 43 | |
|---|
| 44 | point = self.__button.mapToGlobal(QPoint(0, 0)) |
|---|
| 45 | |
|---|
| 46 | # Make sure the dialog fit in the screen horizontally |
|---|
| 47 | if (point.x() + windowWidth > rect.right()): |
|---|
| 48 | point.setX(point.x() + self.__button.width() - windowWidth) |
|---|
| 49 | |
|---|
| 50 | # Make sure the dialog fit in the screen vertically |
|---|
| 51 | if (point.y() + self.__button.height() + windowHeight > rect.bottom()): |
|---|
| 52 | point.setY(point.y() - windowHeight) |
|---|
| 53 | else: |
|---|
| 54 | point.setY(point.y() + self.__button.height()) |
|---|
| 55 | |
|---|
| 56 | self.move(point) |
|---|
| 57 | |
|---|
| 58 | def hideEvent(self, event): |
|---|
| 59 | QFrame.hideEvent(self, event) |
|---|
| 60 | # This is a bit hackish: the window hides itself when one clicks outside it |
|---|
| 61 | # (because it has the Qt.Popup flag). In this case we want to uncheck its |
|---|
| 62 | # associated button. |
|---|
| 63 | |
|---|
| 64 | # On MacOS X we must always uncheck the button ourself. |
|---|
| 65 | # On Linux and Windows, we must not uncheck it ourself if the cursor is |
|---|
| 66 | # over the button, because the button receives the click and will reopen |
|---|
| 67 | # itself. |
|---|
| 68 | |
|---|
| 69 | if not self.__button.isChecked(): |
|---|
| 70 | return |
|---|
| 71 | |
|---|
| 72 | #ifndef OS_MACOSX |
|---|
| 73 | if not platform.system()=="Darwin": |
|---|
| 74 | # Note: Do not use self.__button.underMouse(), because it returns False if the |
|---|
| 75 | # user has clicked on a widget inside the DropDownWindow instance. |
|---|
| 76 | pos = self.__button.mapFromGlobal(QCursor.pos()) |
|---|
| 77 | underMouse = self.__button.rect().contains(pos) |
|---|
| 78 | if underMouse: |
|---|
| 79 | return |
|---|
| 80 | |
|---|
| 81 | #endif |
|---|
| 82 | |
|---|
| 83 | self.__button.blockSignals(True) |
|---|
| 84 | self.__button.setChecked(False) |
|---|
| 85 | self.__button.blockSignals(False) |
|---|
| 86 | |
|---|
| 87 | def buttonToggledSlot(self, on): |
|---|
| 88 | if on: |
|---|
| 89 | self.show() |
|---|
| 90 | else: |
|---|
| 91 | self.hide() |
|---|
| 92 | |
|---|