StelMainGraphicsView.cpp   StelMainGraphicsView.cpp 
skipping to change at line 21 skipping to change at line 21
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include "StelMainGraphicsView.hpp" #include "StelMainGraphicsView.hpp"
#include "StelAppGraphicsScene.hpp" #include "StelAppGraphicsWidget.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelCore.hpp" #include "StelCore.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelProjector.hpp" #include "StelProjector.hpp"
#include "StelModuleMgr.hpp" #include "StelModuleMgr.hpp"
#include "StelScriptMgr.hpp" #include "StelScriptMgr.hpp"
#include "StelPainter.hpp"
#include "StelGuiBase.hpp"
#include "StelMainScriptAPIProxy.hpp"
#include <QGLFormat> #include <QGLFormat>
#include <QPaintEngine>
#include <QGraphicsView> #include <QGraphicsView>
#include <QGLWidget> #include <QGLWidget>
#include <QResizeEvent> #include <QResizeEvent>
#include <QSettings> #include <QSettings>
#include <QCoreApplication> #include <QCoreApplication>
#include <QApplication>
#include <QDebug> #include <QDebug>
#include <QThread> #include <QFileInfo>
#include <QAction> #include <QGraphicsGridLayout>
#include <QRegExp> #include <QGraphicsProxyWidget>
#include "gui/StelGui.hpp"
// Initialize static variables // Initialize static variables
StelMainGraphicsView* StelMainGraphicsView::singleton = NULL; StelMainGraphicsView* StelMainGraphicsView::singleton = NULL;
StelMainGraphicsView::StelMainGraphicsView(QWidget* parent, int argc, char* class StelQGLWidget : public QGLWidget
* argv) {
: QGraphicsView(parent), public:
StelQGLWidget(const QGLFormat& format, QWidget* parent) : QGLWidget(
format, parent)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_OpaquePaintEvent);
//setAutoFillBackground (false);
setBackgroundRole(QPalette::Window);
}
protected:
virtual void initializeGL()
{
QGLWidget::initializeGL();
if (!format().stencil())
qWarning("Could not get stencil buffer; results will
be suboptimal");
if (!format().depth())
qWarning("Could not get depth buffer; results will b
e suboptimal");
if (!format().doubleBuffer())
qWarning("Could not get double buffer; results will
be suboptimal");
QString paintEngineStr;
switch (paintEngine()->type())
{
case QPaintEngine::OpenGL:
paintEngineStr = "OpenGL";
break;
case QPaintEngine::OpenGL2:
paintEngineStr = "OpenGL2";
break;
default:
paintEngineStr = "Other";
}
qDebug() << "Qt GL paint engine is: " << paintEngineStr;
}
};
StelMainGraphicsView::StelMainGraphicsView(QWidget* parent)
: QGraphicsView(parent), backItem(NULL), gui(NULL), scriptAPIProxy(N
ULL), scriptMgr(NULL),
wasDeinit(false), wasDeinit(false),
flagInvertScreenShotColors(false), flagInvertScreenShotColors(false),
screenShotPrefix("stellarium-"), screenShotPrefix("stellarium-"),
screenShotDir("") screenShotDir(""),
cursorTimeout(-1.f), flagCursorTimeout(false), minFpsTimer(NULL),
maxfps(10000.f)
{ {
// Is this line useful?? StelApp::initStatic();
//setScene(new QGraphicsScene(this));
// Can't create 2 StelMainWindow instances // Can't create 2 StelMainGraphicsView instances
Q_ASSERT(!singleton); Q_ASSERT(!singleton);
singleton = this; singleton = this;
setObjectName("Mainview"); setObjectName("Mainview");
// Avoid white background at init // Avoid white background at init
setStyleSheet(QString("QGraphicsView {background: #000;}")); setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_OpaquePaintEvent);
setAutoFillBackground (true);
setBackgroundRole(QPalette::Window);
QPalette pal;
pal.setColor(QPalette::Window, Qt::black);
setPalette(pal);
// Allows for precise FPS control
setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
setFrameShape(QFrame::NoFrame); setFrameShape(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFocusPolicy(Qt::StrongFocus);
connect(this, SIGNAL(screenshotRequested()), this, SLOT(doScreenshot
()));
qtime = new QTime();
qtime->start();
lastEventTimeSec = 0;
// Create an openGL viewport // Create an openGL viewport
QGLFormat glFormat(QGL::StencilBuffer); QGLFormat glFormat(QGL::StencilBuffer | QGL::DepthBuffer | QGL::Doub
//glFormat.setSamples(16); leBuffer);
//glFormat.setSampleBuffers(true); glWidget = new StelQGLWidget(glFormat, this);
//glFormat.setDirectRendering(false); glWidget->updateGL();
glWidget = new QGLWidget(glFormat, NULL);
setViewport(glWidget); setViewport(glWidget);
// Create the main instance of stellarium //setOptimizationFlags(QGraphicsView::DontClipPainter|QGraphicsView:
stelApp = new StelApp(argc, argv); :DontSavePainterState|QGraphicsView::DontAdjustForAntialiasing);
setScene(new StelAppGraphicsScene()); setScene(new QGraphicsScene());
setFocusPolicy(Qt::ClickFocus);
connect(this, SIGNAL(screenshotRequested()), this, SLOT(doScreenshot backItem = new QGraphicsWidget();
())); backItem->setFocusPolicy(Qt::NoFocus);
} }
StelMainGraphicsView::~StelMainGraphicsView() StelMainGraphicsView::~StelMainGraphicsView()
{ {
} }
void StelMainGraphicsView::resizeEvent(QResizeEvent* event) void StelMainGraphicsView::swapBuffer()
{ {
if (scene()) glWidget->swapBuffers();
scene()->setSceneRect(QRect(QPoint(0, 0), event->size())); }
QGraphicsView::resizeEvent(event);
void StelMainGraphicsView::makeGLContextCurrent()
{
Q_ASSERT(glWidget!=NULL);
Q_ASSERT(glWidget->isValid());
glWidget->makeCurrent();
} }
void StelMainGraphicsView::init() void StelMainGraphicsView::init(QSettings* conf)
{ {
QSettings* conf = StelApp::getInstance().getSettings(); Q_ASSERT(glWidget->isValid());
Q_ASSERT(conf); glWidget->makeCurrent();
// Create the main widget for stellarium, which in turn creates the
main StelApp instance.
mainSkyItem = new StelAppGraphicsWidget();
mainSkyItem->setZValue(-10);
QGraphicsGridLayout* l = new QGraphicsGridLayout(backItem);
l->setContentsMargins(0,0,0,0);
l->setSpacing(0);
l->addItem(mainSkyItem, 0, 0);
scene()->addItem(backItem);
// Activate the resizing caused by the layout
QCoreApplication::processEvents();
mainSkyItem->setFocus();
flagInvertScreenShotColors = conf->value("main/invert_screenshots_co lors", false).toBool(); flagInvertScreenShotColors = conf->value("main/invert_screenshots_co lors", false).toBool();
setFlagCursorTimeout(conf->value("gui/flag_mouse_cursor_timeout", fa
lse).toBool());
setCursorTimeout(conf->value("gui/mouse_cursor_timeout", 10.).toDoub
le());
maxfps = conf->value("video/maximum_fps",10000.).toDouble();
minfps = conf->value("video/minimum_fps",10000.).toDouble();
// This apparently useless line fixes the scrolling bug QPainter qPainter(glWidget);
// I suspect a Qt 4.4 bug -> Fixed with Qt 4.4.1 StelPainter::setQPainter(&qPainter);
#if QT_VERSION == 0x040400
setMatrix(QMatrix(1,0,0,1,0.00000001,0));
#endif
// Bug in the Qt 4.5 beta version
#if QT_VERSION == 0x040500
setMatrix(QMatrix(1,0,0,1,0.00000001,0));
#endif
// Antialiasing works only with SampleBuffer, but it's much slower
// setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTra
nsform | QPainter::HighQualityAntialiasing);
//setRenderHint(QPainter::TextAntialiasing, false);
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
stelApp->init();
static_cast<StelAppGraphicsScene*>(scene())->init();
setFocus();
// Allows for precise FPS control // Initialize the core, including the StelApp instance.
setViewportUpdateMode(QGraphicsView::NoViewportUpdate); mainSkyItem->init(conf);
//setOptimizationFlags(QGraphicsView::DontClipPainter|QGraphicsView: // Prevent flickering on mac Leopard/Snow Leopard
:DontSavePainterState|QGraphicsView::DontAdjustForAntialiasing); glWidget->setAutoFillBackground (false);
StelGui* newGui = new StelGui(); scriptAPIProxy = new StelMainScriptAPIProxy(this);
newGui->init(); scriptMgr = new StelScriptMgr(this);
StelApp::getInstance().getModuleMgr().registerModule(newGui, true);
stelApp->initPlugIns(); // Look for a static GUI plugins.
foreach (QObject *plugin, QPluginLoader::staticInstances())
{
StelGuiPluginInterface* pluginInterface = qobject_cast<StelG
uiPluginInterface*>(plugin);
if (pluginInterface)
{
gui = pluginInterface->getStelGuiBase();
}
break;
}
Q_ASSERT(gui); // There was no GUI plugin found
StelApp::getInstance().setGui(gui);
gui->init(backItem, mainSkyItem);
StelApp::getInstance().initPlugIns();
stelApp->getScriptMgr().runScript(stelApp->getStartupScript()); // Force refreshing of button bars if plugins modified the GUI, e.g.
added buttons.
gui->forceRefreshGui();
const QString& startupScript = conf->value("scripts/startup_script",
"startup.ssc").toString();
scriptMgr->runScript(startupScript);
QThread::currentThread()->setPriority(QThread::HighestPriority); QThread::currentThread()->setPriority(QThread::HighestPriority);
StelPainter::setQPainter(NULL);
startMainLoop();
}
void StelMainGraphicsView::thereWasAnEvent()
{
lastEventTimeSec = StelApp::getTotalRunTime();
}
void StelMainGraphicsView::drawBackground(QPainter* painter, const QRectF&
rect)
{
if (painter->paintEngine()->type()!=QPaintEngine::OpenGL && painter-
>paintEngine()->type()!=QPaintEngine::OpenGL2)
{
qWarning("StelMainGraphicsView: drawBackground needs a QGLWi
dget to be set as viewport on the graphics view");
return;
}
const double now = ((double)qtime->elapsed())/1000.;
// Determines when the next display will need to be triggered
// The current policy is that after an event, the FPS is maximum for
2.5 seconds
// after that, it switches back to the default minfps value to save
power
if (now-lastEventTimeSec<2.5)
{
double duration = 1./getMaxFps();
int dur = (int)(duration*1000);
QTimer::singleShot(dur<5 ? 5 : dur, scene(), SLOT(update()))
;
}
// Manage cursor timeout
if (cursorTimeout>0.f && (now-lastEventTimeSec>cursorTimeout) && fla
gCursorTimeout)
{
if (QApplication::overrideCursor()==0)
QApplication::setOverrideCursor(Qt::BlankCursor);
}
else
{
if (QApplication::overrideCursor()!=0)
QApplication::restoreOverrideCursor();
}
//QGraphicsView::drawBackground(painter, rect);
}
void StelMainGraphicsView::drawForeground(QPainter* painter, const QRectF &
rect)
{
//QGraphicsView::drawForeground(painter, rect);
}
void StelMainGraphicsView::startMainLoop()
{
// Set a timer refreshing for every minfps frames
minFpsChanged();
}
void StelMainGraphicsView::minFpsChanged()
{
if (minFpsTimer!=NULL)
{
disconnect(minFpsTimer, SIGNAL(timeout()), 0, 0);
delete minFpsTimer;
minFpsTimer = NULL;
}
minFpsTimer = new QTimer(this);
connect(minFpsTimer, SIGNAL(timeout()), scene(), SLOT(update()));
minFpsTimer->start((int)(1./getMinFps()*1000.));
}
void StelMainGraphicsView::resizeEvent(QResizeEvent* event)
{
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
backItem->setGeometry(0,0,event->size().width(),event->size().height
());
QGraphicsView::resizeEvent(event);
}
void StelMainGraphicsView::mouseMoveEvent(QMouseEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::mouseMoveEvent(event);
}
void StelMainGraphicsView::mousePressEvent(QMouseEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::mousePressEvent(event);
}
void StelMainGraphicsView::mouseReleaseEvent(QMouseEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::mouseReleaseEvent(event);
}
void StelMainGraphicsView::wheelEvent(QWheelEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::wheelEvent(event);
}
void StelMainGraphicsView::keyPressEvent(QKeyEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::keyPressEvent(event);
}
void StelMainGraphicsView::keyReleaseEvent(QKeyEvent* event)
{
thereWasAnEvent(); // Refresh screen ASAP
QGraphicsView::keyReleaseEvent(event);
} }
//! Delete openGL textures (to call before the GLContext disappears) //! Delete openGL textures (to call before the GLContext disappears)
void StelMainGraphicsView::deinitGL() void StelMainGraphicsView::deinitGL()
{ {
// Can be called only once // Can be called only once
if (wasDeinit==true) if (wasDeinit==true)
return; return;
wasDeinit = true; wasDeinit = true;
if (scriptMgr->scriptIsRunning())
scriptMgr->stopScript();
StelApp::getInstance().getModuleMgr().unloadAllPlugins(); StelApp::getInstance().getModuleMgr().unloadAllPlugins();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
StelApp::getInstance().getModuleMgr().unloadModule("StelGui"); delete mainSkyItem;
QCoreApplication::processEvents();
StelApp* stelApp = &StelApp::getInstance();
delete stelApp;
} }
void StelMainGraphicsView::saveScreenShot(const QString& filePrefix, const QString& saveDir) void StelMainGraphicsView::saveScreenShot(const QString& filePrefix, const QString& saveDir)
{ {
screenShotPrefix = filePrefix; screenShotPrefix = filePrefix;
screenShotDir = saveDir; screenShotDir = saveDir;
emit(screenshotRequested()); emit(screenshotRequested());
} }
void StelMainGraphicsView::doScreenshot(void) void StelMainGraphicsView::doScreenshot(void)
{ {
QString shotDir; QFileInfo shotDir;
QImage im = glWidget->grabFrameBuffer(); QImage im = glWidget->grabFrameBuffer();
if (flagInvertScreenShotColors) if (flagInvertScreenShotColors)
im.invertPixels(); im.invertPixels();
if (screenShotDir == "") if (screenShotDir == "")
shotDir = QFileInfo(StelFileMgr::getScreenshotDir());
else
shotDir = QFileInfo(screenShotDir);
if (!shotDir.isDir())
{ {
try qWarning() << "ERROR requested screenshot directory is not a
{ directory: " << shotDir.filePath();
shotDir = StelApp::getInstance().getFileMgr().getScr return;
eenshotDir();
if (!StelApp::getInstance().getFileMgr().isWritable(
shotDir))
{
qWarning() << "ERROR StelMainGraphicsView::s
aveScreenShot: screenshot directory is not writable: " << qPrintable(shotDi
r);
return;
}
}
catch(std::runtime_error& e)
{
qWarning() << "ERROR StelMainGraphicsView::saveScree
nShot: could not determine screenshot directory: " << e.what();
return;
}
} }
else else if (!shotDir.isWritable())
{ {
shotDir = screenShotDir; qWarning() << "ERROR requested screenshot directory is not w
ritable: " << shotDir.filePath();
return;
} }
QString shotPath; QFileInfo shotPath;
for (int j=0; j<100000; ++j) for (int j=0; j<100000; ++j)
{ {
shotPath = shotDir+"/"+ screenShotPrefix + QString("%1").arg shotPath = QFileInfo(shotDir.filePath() + "/" + screenShotPr
(j, 3, 10, QLatin1Char('0')) + ".png"; efix + QString("%1").arg(j, 3, 10, QLatin1Char('0')) + ".png");
if (!StelApp::getInstance().getFileMgr().exists(shotPath)) if (!shotPath.exists())
break; break;
} }
qDebug() << "Saving screenshot in file: " << shotPath; qDebug() << "INFO Saving screenshot in file: " << shotPath.filePath(
im.save(shotPath); );
if (!StelApp::getInstance().getFileMgr().exists(shotPath)) if (!im.save(shotPath.filePath())) {
return; qWarning() << "WARNING failed to write screenshot to: " << s
else hotPath.filePath();
return;
}
// Add a new progress bar in the lower right corner of the screen.
class QProgressBar* StelMainGraphicsView::addProgressBar()
{
StelGui* gui = GETSTELMODULE(StelGui);
return gui->addProgressBar();
}
//! Activate all the QActions associated to the widget
void StelMainGraphicsView::activateKeyActions(bool b)
{
QList<QAction*> globalActions = findChildren<QAction*>(QRegExp("acti
on.*_Global"));
if (b==false)
{
foreach (QAction* a, actions())
{
// Special case for key actions which are named Glob
al
if (!globalActions.contains(a))
removeAction(a);
}
}
else
{
foreach (QAction* a, findChildren<QAction*>())
{
// Special case for key actions which are named Glob
al
if (!globalActions.contains(a))
addAction(a);
}
} }
} }
 End of changes. 36 change blocks. 
131 lines changed or deleted 296 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/