Skip to content

Commit

Permalink
Re #4282. Made window positioning a bit smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Jan 13, 2012
1 parent 208dece commit e32d626
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 29 deletions.
120 changes: 97 additions & 23 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
#include <QUndoStack>
#include <QUndoView>
#include <QSignalMapper>
#include <QDesktopWidget>

#include <zlib.h>

Expand Down Expand Up @@ -5818,7 +5819,7 @@ void ApplicationWindow::restoreWindowGeometry(ApplicationWindow *app, MdiSubWind
}
}

Folder* ApplicationWindow::projectFolder()
Folder* ApplicationWindow::projectFolder() const
{
return dynamic_cast<FolderListItem*>(folders->firstChild())->folder();
}
Expand Down Expand Up @@ -9259,6 +9260,13 @@ void ApplicationWindow::closeEvent( QCloseEvent* ce )

// Mantid changes here

// don't ask the closing sub-windows: the answer will be ignored
MDIWindowList windows = getAllWindows();
foreach(MdiSubWindow* w,windows)
{
w->askOnCloseEvent(false);
}

if( scriptingEnv()->isRunning() )
{
if( QMessageBox::question(this, tr("MantidPlot"), "A script is still running, abort and quit application?", tr("Yes"), tr("No")) == 0 )
Expand Down Expand Up @@ -14070,7 +14078,7 @@ void ApplicationWindow::deleteFitTables()
delete mLst;
}

QList<MdiSubWindow *> ApplicationWindow::windowsList()
QList<MdiSubWindow *> ApplicationWindow::windowsList() const
{
QList<MdiSubWindow *> lst;

Expand All @@ -14084,6 +14092,36 @@ QList<MdiSubWindow *> ApplicationWindow::windowsList()
return lst;
}

/**
* Return all windows in all folders.
*/
QList<MdiSubWindow *> ApplicationWindow::getAllWindows() const
{
QList<MdiSubWindow *> out;
// get the docked windows first
QList<QMdiSubWindow*> wl = d_workspace->subWindowList();
foreach(QMdiSubWindow* w,wl)
{
MdiSubWindow* sw = dynamic_cast<MdiSubWindow*>(w->widget());
if (sw)
{
out.append(sw);
}
}

// get the floating windows
foreach(FloatingWindow* w, m_floatingWindows)
{
MdiSubWindow* sw = w->mdiSubWindow();
if (sw)
{
out.append(sw);
}
}
return out;
}


void ApplicationWindow::updateRecentProjectsList()
{
if (recentProjects.isEmpty())
Expand Down Expand Up @@ -17051,7 +17089,7 @@ QList<QMenu *> ApplicationWindow::menusList()
// End of a section of Mantid custom functions
//-------------------------------------------

QList<QToolBar *> ApplicationWindow::toolBarsList()
QList<QToolBar *> ApplicationWindow::toolBarsList() const
{
QList<QToolBar *> lst;
QObjectList children = this->children();
Expand Down Expand Up @@ -17446,23 +17484,43 @@ void ApplicationWindow::addMdiSubWindow(MdiSubWindow *w, bool showNormal)
/**
* Add a sub-window to as a floating window.
* @param w :: Pointer to a MdiSubWindow which will be wrapped in a FloatingWindow.
* @param pos :: Position of created window relative to the main window
* @param pos :: Position of created window relative to the main window.
* Setting it to (-1,-1) means no autogenerate the position.
*/
FloatingWindow* ApplicationWindow::addMdiSubWindowAsFloating(MdiSubWindow* w, QPoint pos)
{
const QPoint none(-1,-1);
FloatingWindow* fw =new FloatingWindow(this);//, Qt::WindowStaysOnTopHint);
#ifdef SHARED_MENUBAR
if (m_sharedMenuBar != NULL)
{
fw->setMenuBar(m_sharedMenuBar);
}
#endif
// calculate the postion for the new window
QSize sz = w->size();
if (pos == none)
{
pos = positionNewFloatinfWindow(sz);
}
else
{
pos += desktopTopLeft();
}
fw->setWindowTitle(w->name());
fw->setMdiSubWindow(w);
fw->resize(sz);
fw->move(pos);
fw->show();
m_floatingWindows.append(fw);
return fw;
}

/**
* Returns the top-left corner of the desktop available for sub-windows.
*/
QPoint ApplicationWindow::desktopTopLeft() const
{
QPoint p = this->pos() + d_workspace->pos();
QPoint p0 = pos;
if (p0.y() < 0) p0.setY(0);
p += p0;

// make sure the floating window doesn't overlap the tool bars
QList<QToolBar *> toolBars = toolBarsList();
Expand All @@ -17472,14 +17530,36 @@ FloatingWindow* ApplicationWindow::addMdiSubWindowAsFloating(MdiSubWindow* w, QP
int y = this->pos().y() + d_workspace->pos().y() + bar->rect().bottom();
if (y > p.y()) p.setY(y + 1);
}
return p;
}

/**
* Find the best position for a new floating window.
* @param sz :: Size of the new window.
*/
QPoint ApplicationWindow::positionNewFloatinfWindow(QSize sz) const
{
const int dlt = 40; // shift in x and y
const QPoint first(-1,-1);
static QPoint lastPoint(first);

if (lastPoint == first)
{
lastPoint = desktopTopLeft();
return lastPoint;
}

lastPoint += QPoint(dlt,dlt);

QWidget* desktop = QApplication::desktop()->screen();
if (lastPoint.x() + sz.width() > desktop->width() ||
lastPoint.y() + sz.height() > desktop->height())
{
lastPoint = QPoint(0,0);
}

return lastPoint;

fw->setWindowTitle(w->name());
fw->setMdiSubWindow(w);
fw->resize(sz);
fw->move(p);
fw->show();
m_floatingWindows.append(fw);
return fw;
}

/**
Expand Down Expand Up @@ -17619,14 +17699,6 @@ void ApplicationWindow::mdiWindowActivated(MdiSubWindow* w)
setActiveWindow(w);
}

/**
* Update the stays-on-top flags of all floating windows. The active floating window
* will have the flag on, the others - off.
*/
void ApplicationWindow::updateOnTopFlags()
{
}

/**
* Activate a subwindow (docked or floating) other than current active one.
* This is required when the current window is closing.
Expand Down Expand Up @@ -17728,9 +17800,11 @@ bool ApplicationWindow::isDefaultFloating(const MdiSubWindow* w) const
bool ApplicationWindow::isDefaultFloating(const QString& aClassName) const
{
bool theDefault = false;
#ifndef Q_OS_LINUX
if (aClassName == "MultiLayer" || aClassName =="InstrumentWindow" || aClassName == "MdiSubWindow")
{
theDefault = true;
}
#endif
return settings.value("/General/FloatingWindows/"+aClassName,theDefault).toBool();
}
14 changes: 8 additions & 6 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class ApplicationWindow: public QMainWindow, public Scripted
//@}

QList<QMenu *> menusList();
QList<QToolBar *> toolBarsList();
QList<QToolBar *> toolBarsList() const;

MdiSubWindow *activeWindow(WindowType type = NoWindow);
void addMdiSubWindow(MdiSubWindow *w, bool showNormal = true);
Expand Down Expand Up @@ -225,7 +225,8 @@ public slots:
*/
ApplicationWindow * loadScript(const QString& fn, bool execute = false, bool quit = false);

QList<MdiSubWindow *> windowsList();
QList<MdiSubWindow *> windowsList() const;
QList<MdiSubWindow *> getAllWindows() const;
void updateWindowLists(MdiSubWindow *w);
/**
Arranges all the visible project windows in a cascade pattern.
Expand Down Expand Up @@ -974,7 +975,7 @@ public slots:
void setShowWindowsPolicy(int p);

//! returns a pointer to the root project folder
Folder* projectFolder();
Folder* projectFolder() const;

//! used by the findDialog
void find(const QString& s, bool windowNames, bool labels, bool folderNames,
Expand Down Expand Up @@ -1040,7 +1041,7 @@ public slots:
void activateNewWindow();

// Methods for Floating windows
FloatingWindow* addMdiSubWindowAsFloating(MdiSubWindow* w, QPoint pos = QPoint(0,0));
FloatingWindow* addMdiSubWindowAsFloating(MdiSubWindow* w, QPoint pos = QPoint(-1,-1));
QMdiSubWindow* addMdiSubWindowAsDocked(MdiSubWindow* w);
void mdiWindowActivated(MdiSubWindow* w);
void changeToFloating(MdiSubWindow* w);
Expand Down Expand Up @@ -1072,7 +1073,7 @@ public slots:
virtual bool event(QEvent * e);

private:
virtual QMenu * createPopupMenu(){return NULL;};
virtual QMenu * createPopupMenu(){return NULL;}
///void open spectrogram plot from project
Spectrogram* openSpectrogram(Graph*ag,const std::string &wsName,const QStringList &lst);
Matrix* openMatrix(ApplicationWindow* app, const QStringList &flist);
Expand All @@ -1084,7 +1085,8 @@ public slots:
void openInstrumentWindow(const QStringList &list);
/// this method saves the data on project save
void savedatainNexusFormat(const std::string& wsName,const std::string & fileName);
void updateOnTopFlags();
QPoint positionNewFloatinfWindow(QSize sz) const;
QPoint desktopTopLeft() const;


private slots:
Expand Down

0 comments on commit e32d626

Please sign in to comment.