1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*LICENSE_START*/
/*
* Copyright 1995-2002 Washington University School of Medicine
*
* http://brainmap.wustl.edu
*
* This file is part of CARET.
*
* CARET is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CARET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CARET; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*LICENSE_END*/
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QDialogButtonBox>
#include <QImage>
#include <QMenu>
#include <QPixmap>
#include <QPushButton>
#include <QTimer>
#include "WuQMessageBox.h"
/**
* constructor.
*/
WuQMessageBox::WuQMessageBox(QWidget* parent)
: QMessageBox(parent)
{
}
/**
* constructor.
*/
WuQMessageBox::WuQMessageBox(Icon icon,
const QString& title,
const QString& text,
StandardButtons buttons,
QWidget* parent,
Qt::WindowFlags f)
: QMessageBox(icon,
title,
text,
buttons,
parent,
f)
{
setTheWindowTitle(title);
}
/**
* destructor.
*/
WuQMessageBox::~WuQMessageBox()
{
}
/**
* Set the window title.
* Mac Guidelines say that a pop-up should not have a title so Qt's
* implementation of QMessageBox has its own setWindowTitle() method
* that overrides QDialog::setWindowTitle() to prevent the title
* from showing up on a Mac. But, we want the title on Macs, and
* this method does it!
*/
void
WuQMessageBox::setTheWindowTitle(const QString& title)
{
QDialog::setWindowTitle(title);
}
/**
* called to capture image after timeout so nothing obscures window. */
void
WuQMessageBox::slotCaptureImageAfterTimeOut()
{
QImage image = QPixmap::grabWindow(this->winId()).toImage();
if (image.isNull() == false) {
QClipboard* clipboard = QApplication::clipboard();
clipboard->setImage(image);
QMessageBox::information(this,
"Information",
"An image of this dialog has been placed onto the computer's clipboard.");
}
}
/**
* called to capture image of window and place it on the clipboard
*/
void
WuQMessageBox::slotMenuCaptureImageOfWindowToClipboard()
{
//
// Need to delay capture so that the context sensistive
// menu closes or else the menu will be in the captured image.
//
QApplication::processEvents();
QTimer::singleShot(1000, this, SLOT(slotCaptureImageAfterTimeOut()));
}
/**
* called by parent when context menu event occurs.
*/
void
WuQMessageBox::contextMenuEvent(QContextMenuEvent* cme)
{
//
// Popup menu for selection of pages
//
QMenu menu(this);
//
// Add menu item for image capture
//
menu.addAction("Capture Image to Clipboard",
this,
SLOT(slotMenuCaptureImageOfWindowToClipboard()));
//
// Popup the menu
//
menu.exec(cme->globalPos());
}
QMessageBox::StandardButton
WuQMessageBox::information(QWidget *parent, const QString &title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return showNewMessageBox(parent, Information, title, text, buttons,
defaultButton);
}
QMessageBox::StandardButton
WuQMessageBox::question(QWidget *parent, const QString &title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
}
QMessageBox::StandardButton
WuQMessageBox::warning(QWidget *parent, const QString &title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
}
QMessageBox::StandardButton
WuQMessageBox::critical(QWidget *parent, const QString &title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
}
/**
* used by other static methods to create the dialog.
*/
QMessageBox::StandardButton
WuQMessageBox::showNewMessageBox(QWidget *parent,
QMessageBox::Icon icon,
const QString& title,
const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
// necessary for source compatibility with Qt 4.0 and 4.1
// handles (Yes, No) and (Yes|Default, No)
if (defaultButton && !(buttons & defaultButton)) {
return (QMessageBox::StandardButton)
showOldMessageBox(parent, icon, title,
text, int(buttons),
int(defaultButton), 0);
}
WuQMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
msgBox.setTheWindowTitle(title);
QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&msgBox);
Q_ASSERT(buttonBox != 0);
uint mask = QMessageBox::FirstButton;
while (mask <= QMessageBox::LastButton) {
uint sb = buttons & mask;
mask <<= 1;
if (!sb)
continue;
QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
// Choose the first accept role as the default
if (msgBox.defaultButton())
continue;
if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
|| (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
msgBox.setDefaultButton(button);
}
if (msgBox.exec() == -1)
return QMessageBox::Cancel;
return msgBox.standardButton(msgBox.clickedButton());
}
/**
* Show the old message box
*/
int
WuQMessageBox::showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
const QString &title, const QString &text,
int button0, int button1, int button2)
{
int butts[3] = { button0, button1, button2 };
QMessageBox::StandardButtons buttons;
for (int i = 0; i < 3; i++) {
switch (butts[i]) {
case QMessageBox::YesAll:
buttons |= QMessageBox::YesToAll;
break;
case QMessageBox::NoAll:
buttons |= QMessageBox::NoToAll;
break;
default:
buttons |= static_cast<QMessageBox::StandardButton>(butts[i]);
break;
}
}
WuQMessageBox messageBox(icon, title, text, buttons, parent);
return messageBox.exec();
}
|