Skip to content

Commit

Permalink
Re #4282. Working progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Dec 21, 2011
1 parent 19c3899 commit baff030
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 112 deletions.
171 changes: 72 additions & 99 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void file_uncompress(const char *file);


ApplicationWindow::ApplicationWindow(bool factorySettings)
: QMainWindow(), Scripted(ScriptingLangManager::newEnv(this)),xxx(NULL)
: QMainWindow(), Scripted(ScriptingLangManager::newEnv(this)),blockWindowActivation(false)
{
QCoreApplication::setOrganizationName("ISIS");
QCoreApplication::setApplicationName("MantidPlot");
Expand Down Expand Up @@ -3391,29 +3391,6 @@ void ApplicationWindow::windowActivated(QMdiSubWindow *w)

activateWindow(qti_subwin);

//if( d_active_window && d_active_window == qti_subwin ) return;

//d_active_window = qti_subwin;
//customToolBars(qti_subwin);
//customMenu(qti_subwin);

//if (d_opening_file) return;

//QList<MdiSubWindow *> windows = current_folder->windowsList();
//foreach(MdiSubWindow *ow, windows){
// QMdiSubWindow* qw = dynamic_cast<QMdiSubWindow*>(ow->parent());
// if (qw && qw != w && qw->isMaximized())
// {
// ow->setNormal();
// break;
// }
//}

//Folder *f = qti_subwin->folder();
//if (f)
// f->setActiveWindow(qti_subwin);

//emit modified();
}

void ApplicationWindow::addErrorBars()
Expand Down Expand Up @@ -8384,18 +8361,43 @@ void ApplicationWindow::activateWindow()
activateWindow(it->window());
}

/**
* Activate a new MdiSubWindow: update the menu, tool bars, and folders.
* @param w :: Subwindow to activate.
*/
void ApplicationWindow::activateWindow(MdiSubWindow *w)
{
if (!w) return;

if (blockWindowActivation) return;

if( d_active_window == w ) return;

//FloatingWindow* fw = getActiveFloating();
//if (fw)
//{
// blockWindowActivation = true;
// removeStaysOnTopFlag(fw);
// blockWindowActivation = false;
//}

d_active_window = w;

if (!w) return;

//fw = getActiveFloating();
//if (fw)
//{
// blockWindowActivation = true;
// setStaysOnTopFlag(fw);
// blockWindowActivation = false;
//}

QMdiSubWindow* qw = dynamic_cast<QMdiSubWindow*>(w->parent());
if (qw)
{
qw->showNormal();
}
d_active_window = w;

updateWindowLists(w);

//----------------------------------------------------
Expand Down Expand Up @@ -17270,7 +17272,7 @@ void ApplicationWindow::goFloat(MdiSubWindow* w)
connect(goMdi,SIGNAL(triggered()),w,SLOT(goMdi()));

// create the outer floating window. It is not a child of ApplicationWindow.
FloatingWindow* fw =new FloatingWindow(this, Qt::WindowStaysOnTopHint);
FloatingWindow* fw =new FloatingWindow(this);//, Qt::WindowStaysOnTopHint);
MdiSubWindowParent_t* wrapper = new MdiSubWindowParent_t(fw,0);
wrapper->setWidget(w);
fw->setCentralWidget(wrapper);
Expand Down Expand Up @@ -17328,7 +17330,7 @@ void ApplicationWindow::goMdi(FloatingWindow* fw)
* Put the stays-on-top flag for a window.
* @param fw :: The window to apply the flag to.
*/
void ApplicationWindow::setStaysOnTopFlag(FloatingWindow* fw)const
void ApplicationWindow::setStaysOnTopFlag(FloatingWindow* fw)
{
fw->setStaysOnTopFlag();
}
Expand All @@ -17337,9 +17339,10 @@ void ApplicationWindow::setStaysOnTopFlag(FloatingWindow* fw)const
* Remove the stays-on-top flag for a window.
* @param fw :: The window to remove the flag from.
*/
void ApplicationWindow::removeStaysOnTopFlag(FloatingWindow* fw)const
void ApplicationWindow::removeStaysOnTopFlag(FloatingWindow* fw)
{
fw->removeStaysOnTopFlag();
this->activateWindow();
}

/**
Expand Down Expand Up @@ -17374,69 +17377,62 @@ bool ApplicationWindow::eventFilter(QObject *obj, QEvent *evt)
return false;
}

/**
* Return a pointer to the active FloatingWindow if the active window is floating
* or NULL otherwise.
*/
FloatingWindow* ApplicationWindow::getActiveFloating() const
{
if (!d_active_window || !d_active_window->parent()) return NULL;
return dynamic_cast<FloatingWindow*>(d_active_window->parent()->parent());
}

bool ApplicationWindow::event(QEvent * e)
{
if (e->type() == QEvent::WindowActivate)
{
QPoint cur_pos = this->mapFromGlobal(QCursor::pos()) - menuBar()->rect().bottomLeft();
std::cerr << "Menu bar: ";
if (menuBar()->parent())
{
std::cerr << "parent " << menuBar()->parent()->className() << std::endl;
}
else
{
std::cerr << "parentless\n";
}
const QWidget* clickedWidget = NULL;
std::cerr << "pos: " << cur_pos.x() << ',' << cur_pos.y() << std::endl;
bool needToActivate = true;

QObjectList objects = this->children();
foreach(const QObject* obj,objects)
// check if old active window is a floating one and this window was activated by clicking
// on a tool bar - in this case we shouldn't actvate another window
if (getActiveFloating())
{
const QToolBar* tb = dynamic_cast<const QToolBar*>(obj);
if (!tb || !tb->isVisible()) continue;
std::cerr << obj->className()
<< ' ' << tb->x() << ',' << tb->y() << ',' << tb->width() << ',' << tb->height()
<< std::endl;
if (tb->rect().contains(cur_pos))
QPoint cur_pos = this->mapFromGlobal(QCursor::pos());
const QWidget* clickedWidget = NULL;

if (rect().contains(cur_pos))
{
clickedWidget = childAt(cur_pos);
}

clickedWidget = tb;
break;
if (clickedWidget)
{
QString class_name = clickedWidget->className();
if (class_name == "QToolButton" || class_name == "QToolBar" || class_name == "QMenuBar")
{
needToActivate = false;
}
}
}

std::cerr << "ApplicationWindow activated\n";
if (clickedWidget)
{
std::cerr << "Cursor: " << cur_pos.x() << ',' << cur_pos.y() << ' '
<< pos().x() << ',' << pos().y() << ' '
<< childrenRect().topLeft().x() << ',' << childrenRect().topLeft().y() << std::endl;
std::cerr << "Clicked at " << clickedWidget->className() << ' '
<< clickedWidget->pos().x() << ',' << clickedWidget->pos().y() << std::endl;
MdiSubWindow* sw = d_active_window;
if (sw && sw->parent() && dynamic_cast<FloatingWindow*>(sw->parent()->parent()))
if (needToActivate)
{// activate current MDI subwindow
QMdiSubWindow* qCurrent = d_workspace->currentSubWindow();
if (qCurrent)
{
std::cerr << sw->parent()->parent()->className() << std::endl;
xxx = clickedWidget;
MdiSubWindow* sw = dynamic_cast<MdiSubWindow*>(qCurrent->widget());
if (!sw)
{// this should never happen - all MDI subwindow widgets must inherit from MdiSubwindow
throw std::runtime_error("Non-MdiSubwindow widget found in MDI area");
}
activateWindow(sw);
}
}
QMdiSubWindow* qCurrent = d_workspace->currentSubWindow();
if (qCurrent)
{
std::cerr << "Current " << qCurrent->windowTitle().toStdString() << std::endl;
MdiSubWindow* sw = dynamic_cast<MdiSubWindow*>(qCurrent->widget());
if (!sw)
{// this should never happen - all MDI subwindow widgets must inherit from MdiSubwindow
throw std::runtime_error("Non-MdiSubwindow widget found in MDI area");
else
{
activateWindow(NULL);
}
activateWindow(sw);
}
}
if (e->type() == QEvent::FocusIn)
{
}
return QMainWindow::event(e);
}

Expand Down Expand Up @@ -17467,26 +17463,3 @@ void ApplicationWindow::showActiveWindowInTitle()
}
}

void ApplicationWindow::paintEvent(QPaintEvent* e)
{
QMainWindow::paintEvent(e);
QPainter p(this);
p.setPen(Qt::red);
if (xxx)
{
p.drawRect(xxx->x(),xxx->y(),10,10);
//std::cerr << xxx->x() << ',' << xxx->y() << ',' << xxx->width() << ',' << xxx->height()
// << std::endl;
}
//std::cerr << "--------------------------------\n";
//QObjectList objects = this->children();
//foreach(const QObject* obj,objects)
//{
// const QToolBar* w = dynamic_cast<const QToolBar*>(obj);
// if (!w || !w->isVisible()) continue;
// std::cerr << obj->className()
// << ' ' << w->x() << ',' << w->y() << ',' << w->width() << ',' << w->height()
// << std::endl;
// p.drawRect(w->x(),w->y(),10,10);
//}
}
9 changes: 5 additions & 4 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ public slots:
void dropEvent( QDropEvent* e );
void customEvent( QEvent* e);
bool eventFilter(QObject *obj, QEvent *event);
void paintEvent ( QPaintEvent * event );
//@}

//! \name Dialogs
Expand Down Expand Up @@ -1025,9 +1024,10 @@ public slots:
void mdiWindowActivated(MdiSubWindow* w);
void goFloat(MdiSubWindow* w);
void goMdi(FloatingWindow* w);
void setStaysOnTopFlag(FloatingWindow* w)const;
void removeStaysOnTopFlag(FloatingWindow* w)const;
void setStaysOnTopFlag(FloatingWindow* w);
void removeStaysOnTopFlag(FloatingWindow* w);
void removeFloatingWindow(FloatingWindow* w);
FloatingWindow* getActiveFloating() const;
void showActiveWindowInTitle();

signals:
Expand Down Expand Up @@ -1427,9 +1427,10 @@ public slots:

// Floating windows
QList<FloatingWindow*> m_floatingWindows;
// To block activating new window when a floating window is in process of resetting flags
bool blockWindowActivation;

public:
MantidUI *mantidUI;
const QWidget* xxx;
};
#endif
45 changes: 37 additions & 8 deletions Code/Mantid/MantidPlot/src/MdiSubWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,13 @@ void MdiSubWindow::goMdi()
/**
* Constructor.
*/
FloatingWindow::FloatingWindow(ApplicationWindow* appWindow, Qt::WindowFlags f):QMainWindow(NULL,f)
FloatingWindow::FloatingWindow(ApplicationWindow* appWindow, Qt::WindowFlags f):
QMainWindow(appWindow,f),
d_app(appWindow)
{
setFocusPolicy(Qt::StrongFocus);
connect(appWindow,SIGNAL(shutting_down()),this,SLOT(close()));
m_flags = windowFlags();
}

/**
Expand All @@ -374,26 +377,52 @@ bool FloatingWindow::event(QEvent * e)
{
w = dynamic_cast<MdiSubWindow*>(w_t->widget());
}
if (w)
if (w && this != w->d_app->getActiveFloating())
{
w->d_app->activateWindow(w);
}
}
else if (e->type() == QEvent::WindowStateChange)
{
if (this->isMinimized())
{
this->setParent(NULL);
this->showMinimized();
}
else if ( (!this->isMaximized() || !this->isMinimized() ) && this->parent() != d_app)
{
this->setParent(d_app);
this->setWindowFlags(m_flags);
this->showNormal();
}
else if (this->isMaximized() && this->parent() != d_app)
{
this->setParent(d_app);
this->setWindowFlags(m_flags);
this->showMaximized();
}
}
return QMainWindow::event(e);
}

void FloatingWindow::setStaysOnTopFlag()
{
Qt::WindowFlags flags = windowFlags();
flags |= Qt::WindowStaysOnTopHint;
setWindowFlags(flags);
show();
Qt::WindowFlags new_flags = flags | Qt::WindowStaysOnTopHint;
if (new_flags != flags)
{
setWindowFlags(new_flags);
show();
}
}

void FloatingWindow::removeStaysOnTopFlag()
{
Qt::WindowFlags flags = windowFlags();
flags ^= Qt::WindowStaysOnTopHint;
setWindowFlags(flags);
show();
Qt::WindowFlags new_flags = flags ^ Qt::WindowStaysOnTopHint;
if (new_flags != flags)
{
setWindowFlags(new_flags);
show();
}
}
4 changes: 3 additions & 1 deletion Code/Mantid/MantidPlot/src/MdiSubWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ class FloatingWindow: public QMainWindow
{
Q_OBJECT
public:
FloatingWindow(ApplicationWindow* appWindow, Qt::WindowFlags f);
FloatingWindow(ApplicationWindow* appWindow, Qt::WindowFlags f = 0);
void setStaysOnTopFlag();
void removeStaysOnTopFlag();
protected:
virtual bool event(QEvent * e);
ApplicationWindow* d_app;
Qt::WindowFlags m_flags;
};

#endif

0 comments on commit baff030

Please sign in to comment.