#include "TQtWidget.h"
#include "TQtClientWidget.h"
#include "TQtClientFilter.h"
#include "TQtClientGuard.h"
#include "TGQt.h"
#include "TQtLock.h"
#include <qkeysequence.h>
#if QT_VERSION < 0x40000
# include <qaccel.h>
# include <qobjectlist.h>
#else /* QT_VERSION */
# include <q3accel.h>
# include <qobject.h>
# include <QKeyEvent>
# include <QCloseEvent>
#endif /* QT_VERSION */
#include <qevent.h>
#include "TGClient.h"
TQtClientWidget::TQtClientWidget(TQtClientGuard *guard, QWidget* parent, const char* name, Qt::WFlags f ):
#if QT_VERSION < 0x40000
QFrame(parent,name,f)
#else
QFrame(parent,f)
#endif
,fGrabButtonMask(kAnyModifier), fGrabEventPointerMask(kNoEventMask)
,fGrabEventButtonMask(kNoEventMask), fSelectEventMask(kNoEventMask), fSaveSelectInputMask(kNoEventMask)
,fButton(kAnyButton),fGrabbedKey(0), fPointerOwner(kFALSE)
,fNormalPointerCursor(0),fGrabPointerCursor(0),fGrabButtonCursor(0)
,fIsClosing(false) ,fDeleteNotify(false), fGuard(guard)
,fCanvasWidget(0),fMyRootWindow(0),fEraseColor(0), fErasePixmap(0)
{
#if QT_VERSION >= 0x40000
setName(name);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_PaintOutsidePaintEvent);
#endif
}
TQtClientWidget::~TQtClientWidget()
{
TQtClientFilter *f = gQt->QClientFilter();
if (f) f->GrabPointer(this, 0, 0, 0, kFALSE);
disconnect();
if (fGuard) fGuard->DisconnectChildren(this);
fNormalPointerCursor = 0;
UnSetButtonMask(true);
UnSetKeyMask();
delete fEraseColor; fEraseColor = 0;
delete fErasePixmap; fErasePixmap = 0;
if (!IsClosing())
gQt->SendDestroyEvent(this);
}
void TQtClientWidget::closeEvent(QCloseEvent *ev)
{
printf("TQtClientWidget::closeEvent(QCloseEvent *ev)\n");
QWidget::closeEvent(ev);
}
void TQtClientWidget::setEraseColor(const QColor &color)
{
if (!fEraseColor)
fEraseColor = new QColor(color);
else
*fEraseColor = color;
QPalette pp = palette();
pp.setColor(QPalette::Window, *fEraseColor);
setPalette(pp);
}
void TQtClientWidget::setErasePixmap (const QPixmap &pixmap)
{
if (!fErasePixmap)
fErasePixmap = new QPixmap(pixmap);
else
*fErasePixmap = pixmap;
QPalette pp = palette();
pp.setBrush(QPalette::Window, QBrush(*fErasePixmap));
setPalette(pp);
}
bool TQtClientWidget::IsGrabbed(Event_t &ev)
{
bool grab = false;
QWidget *parent = parentWidget();
if ( ButtonEventMask()
&& !isHidden()
&& !( parent
&& dynamic_cast<TQtClientWidget*>(parent)
&& ((TQtClientWidget *)parent)->IsGrabbed(ev)
)
)
{
bool mask = (ev.fState & fGrabButtonMask) || (fGrabButtonMask & kAnyModifier);
if ((fButton == kAnyButton) && mask)
grab = true;
else
grab = (fButton == EMouseButton(ev.fCode)) && mask;
TQtClientWidget *w = (TQtClientWidget *)TGQt::wid(ev.fWindow);
if (grab && (w != this) ) {
QRect absRect = geometry();
QPoint absPos = mapToGlobal(QPoint(0,0));
absRect.moveTopLeft(absPos);
grab = absRect.contains(ev.fXRoot,ev.fYRoot);
}
if (grab) GrabEvent(ev);
}
return grab;
}
TQtClientWidget *TQtClientWidget::IsKeyGrabbed(const Event_t &ev)
{
TQtClientWidget *grabbed = 0;
UInt_t modifier = ev.fState;
if (SetKeyMask(ev.fCode, modifier, kTestKey)) grabbed = this;
if (grabbed && ( ev.fType == kKeyRelease)) {
SetKeyMask(ev.fCode, modifier, kRemove);
}
TQtClientWidget *wg = this;
if (!grabbed) {
do {
wg = (TQtClientWidget *)wg->parentWidget();
} while ( wg && (grabbed = wg->IsKeyGrabbed(ev)) );
}
if (!grabbed) {
#if QT_VERSION < 0x40000
const QObjectList *childList = children();
if (childList) {
QObjectListIterator next(*childList);
while((wg = dynamic_cast<TQtClientWidget *>(next.current())) && !(grabbed=wg->IsKeyGrabbed(ev)) ) ++next;
#else /* QT_VERSION */
const QObjectList &childList = children();
if (!childList.isEmpty()) {
QListIterator<QObject*> next(childList);
while(next.hasNext() && (wg = dynamic_cast<TQtClientWidget *>(next.next ())) && !(grabbed=wg->IsKeyGrabbed(ev)) ){;}
#endif /* QT_VERSION */
}
}
return grabbed;
}
void TQtClientWidget::GrabEvent(Event_t &ev, bool )
{
TQtClientWidget *w = (TQtClientWidget *)TGQt::wid(ev.fWindow);
if (w != this) {
QPoint mapped = mapFromGlobal(QPoint(ev.fXRoot,ev.fYRoot));
ev.fX = mapped.x();
ev.fY = mapped.y();
ev.fWindow = TGQt::wid(this);
}
}
void TQtClientWidget::SelectInput (UInt_t evmask)
{
fSelectEventMask=evmask;
assert(fSelectEventMask != (UInt_t) -1);
setMouseTracking( fSelectEventMask & kPointerMotionMask );
}
void TQtClientWidget::SetButtonMask(UInt_t modifier,EMouseButton button)
{
fGrabButtonMask = modifier; fButton = button;
TQtClientFilter *f = gQt->QClientFilter();
if (f) {
f->AppendButtonGrab(this);
connect(this,SIGNAL(destroyed(QObject *)),f,SLOT(RemoveButtonGrab(QObject *)));
}
}
void TQtClientWidget::UnSetButtonMask(bool dtor)
{
if (fGrabButtonMask) {
fGrabButtonMask = 0;
TQtClientFilter *f = gQt->QClientFilter();
if (f) {
if ( !dtor ) disconnect(this,SIGNAL(destroyed(QObject *)),f,SLOT(RemoveButtonGrab(QObject *)));
f->RemoveButtonGrab(this);
}
}
}
Bool_t TQtClientWidget::SetKeyMask(Int_t keycode, UInt_t modifier, int insert)
{
Bool_t found = kTRUE;
int key[5]= {0,0,0,0,0};
int ikeys = 0;
int index = 0;
if (keycode) {
if (modifier & kKeyShiftMask) { key[index++] = Qt::SHIFT; ikeys += Qt::SHIFT;}
if (modifier & kKeyLockMask) { key[index++] = Qt::META; ikeys += Qt::META; }
if (modifier & kKeyControlMask) { key[index++] = Qt::CTRL; ikeys += Qt::CTRL; }
if (modifier & kKeyMod1Mask) { key[index++] = Qt::ALT; ikeys += Qt::ALT; }
key[index++] = Qt::UNICODE_ACCEL + keycode; ikeys += Qt::UNICODE_ACCEL + keycode;
}
QKeySequence keys(ikeys);
assert(index<=4);
switch (insert) {
case kInsert:
if (keycode) {
if (!fGrabbedKey) {
#if QT_VERSION < 0x40000
fGrabbedKey = new QAccel(this);
#else /* QT_VERSION */
fGrabbedKey = new Q3Accel(this);
#endif /* QT_VERSION */
connect(fGrabbedKey,SIGNAL(activated ( int )),this,SLOT(Accelerate(int)));
}
if (fGrabbedKey->findKey(keys) == -1) {
fGrabbedKey->insertItem(keys);
}
}
break;
case kRemove:
if (!fGrabbedKey) break;
if (keycode) {
int id = fGrabbedKey->findKey(keys);
if (id != -1) { fGrabbedKey->removeItem(id); }
if (fGrabbedKey->count() == 0) {
delete fGrabbedKey; fGrabbedKey = 0;
}
} else {
delete fGrabbedKey; fGrabbedKey = 0;
}
break;
case kTestKey:
if (fGrabbedKey) {
found = (fGrabbedKey->findKey(keys) != -1);
}
break;
default: break;
}
return found;
}
void TQtClientWidget::SetCanvasWidget(TQtWidget *widget)
{
TQtLock lock;
if (fCanvasWidget)
disconnect(fCanvasWidget,SIGNAL(destroyed()), this, SLOT(disconnect()));
fCanvasWidget = widget;
if (fCanvasWidget) {
#if QT_VERSION < 0x40000
setWFlags(getWFlags () | Qt::WRepaintNoErase | Qt:: WResizeNoErase );
#endif /* QT_VERSION */
connect(fCanvasWidget,SIGNAL(destroyed()),this,SLOT(Disconnect()));
}
}
void TQtClientWidget::UnSetKeyMask(Int_t keycode, UInt_t modifier)
{
SetKeyMask(keycode, modifier, kRemove);
}
void TQtClientWidget::Accelerate(int id)
{
QKeySequence key = fGrabbedKey->key(id);
int l = key.count();
int keycode = key[l-1];
uint state =0;
#if QT_VERSION < 0x40000
if (keycode & Qt::SHIFT) state |= Qt::ShiftButton;
if (keycode & Qt::META) state |= Qt::MetaButton;
if (keycode & Qt::CTRL) state |= Qt::ControlButton;
if (keycode & Qt::ALT) state |= Qt::AltButton;
#else /* QT_VERSION */
if (keycode & Qt::SHIFT) state |= Qt::ShiftModifier;
if (keycode & Qt::META) state |= Qt::MetaModifier;
if (keycode & Qt::CTRL) state |= Qt::ControlModifier;
if (keycode & Qt::ALT) state |= Qt::AltModifier;
#endif /* QT_VERSION */
QKeyEvent ac(QEvent::KeyPress,keycode,keycode,state);
TQtClientFilter *f = gQt->QClientFilter();
if (f) f->AddKeyEvent(ac,this);
QKeyEvent acRelease(QEvent::KeyRelease,keycode,keycode,state);
if (f) f->AddKeyEvent(acRelease,this);
}
void TQtClientWidget::Disconnect()
{
SetCanvasWidget(0); }
void TQtClientWidget::paintEvent( QPaintEvent *e )
{
QFrame::paintEvent(e);
#if ROOT_VERSION_CODE >= ROOT_VERSION(9,15,9)
if (gClient) {
if (!fMyRootWindow)
fMyRootWindow = gClient->GetWindowById(TGQt::rootwid(this));
if (fMyRootWindow) {
gClient->NeedRedraw(fMyRootWindow,kTRUE);
}
}
#endif
}
void TQtClientWidget::polish()
{
QWidget::polish();
}