mainwindow.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <QFile>
  2. #include <QWebSettings>
  3. #include <QDebug>
  4. #include <QDesktopWidget>
  5. #include <QApplication>
  6. #include <QMenuBar>
  7. #include <QMenu>
  8. #include <QAction>
  9. #include <QFileInfo>
  10. #include <QFileDialog>
  11. #include <QVBoxLayout>
  12. #include <QLabel>
  13. #include <QPushButton>
  14. #include "mainwindow.h"
  15. #include "graphicshybridview.h"
  16. MainWindow::MainWindow(QString debug, QWidget *parent)
  17. : QMainWindow(parent),
  18. m_hybridView(0),
  19. m_panningEnabled(false)
  20. {
  21. QWidget *layoutWidget = new QWidget(this);
  22. QVBoxLayout *layout = new QVBoxLayout();
  23. layoutWidget->setLayout(layout);
  24. QLabel *message = new QLabel("No valid widget given as argument. Choose a widget's main HTML file.\n\nPassed arguments: " + debug);
  25. layout->addWidget(message);
  26. QPushButton *fileButton = new QPushButton(tr("Choose a file"));
  27. connect(fileButton, SIGNAL(clicked()), this, SLOT(selectFile()));
  28. layout->addWidget(fileButton);
  29. setWindowTitle("Widget Launcher");
  30. setCentralWidget(layoutWidget);
  31. }
  32. MainWindow::MainWindow(QString widgetIdentifier, QString filePath, bool panningEnabled, QWidget *parent)
  33. : QMainWindow(parent),
  34. m_hybridView(0),
  35. m_view(this),
  36. m_panningEnabled(panningEnabled)
  37. {
  38. openWidget(widgetIdentifier, filePath);
  39. }
  40. void MainWindow::selectFile()
  41. {
  42. QFileInfo f(QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::currentPath(), tr("HTML files (*.html *.htm)")));
  43. if (QFile::exists(f.canonicalFilePath())) {
  44. openWidget("widget-identifier", f.canonicalFilePath());
  45. }
  46. }
  47. void MainWindow::openWidget(QString widgetIdentifier, QString filePath)
  48. {
  49. m_hybridView = new GraphicsHybridView(this, 0, widgetIdentifier);
  50. // If panning is enabled, make sure the web view is resized when so required.
  51. if (m_panningEnabled)
  52. {
  53. connect(m_hybridView, SIGNAL(contentsSizeChanged(QSize)),
  54. this, SLOT(contentsSizeChanged(QSize)));
  55. }
  56. m_hybridView->load(QUrl::fromLocalFile(filePath));
  57. QMenu *resolutionMenu = menuBar()->addMenu(tr("Resolution"));
  58. QAction *w240h320 = new QAction(tr("240 x 320 (E75)"), this);
  59. connect(w240h320, SIGNAL(triggered()), this, SLOT(resizeW240H320()));
  60. resolutionMenu->addAction(w240h320);
  61. QAction *w320x240 = new QAction(tr("320 x 240 (E71)"), this);
  62. connect(w320x240, SIGNAL(triggered()), this, SLOT(resizeW320H240()));
  63. resolutionMenu->addAction(w320x240);
  64. QAction *w360h640 = new QAction(tr("360 x 640 (N97 portrait)"), this);
  65. connect(w360h640, SIGNAL(triggered()), this, SLOT(resizeW360H640()));
  66. resolutionMenu->addAction(w360h640);
  67. QAction *w640h360 = new QAction(tr("640 x 360 (N97 landscape)"), this);
  68. connect(w640h360, SIGNAL(triggered()), this, SLOT(resizeW640H360()));
  69. resolutionMenu->addAction(w640h360);
  70. QAction *w800h480 = new QAction(tr("800 x 480 (N900)"), this);
  71. connect(w800h480, SIGNAL(triggered()), this, SLOT(resizeW800H480()));
  72. resolutionMenu->addAction(w800h480);
  73. if (m_panningEnabled)
  74. {
  75. m_view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  76. m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  77. m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  78. m_view.setFrameStyle(QFrame::NoFrame);
  79. m_pannableView = new PannableView(Qt::Vertical | Qt::Horizontal);
  80. m_pannableView->setWidget(m_hybridView);
  81. m_scene.addItem(m_pannableView);
  82. m_view.setScene(&m_scene);
  83. }
  84. else
  85. {
  86. m_scene.addItem(m_hybridView);
  87. }
  88. m_view.setScene(&m_scene);
  89. m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  90. m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  91. setCentralWidget(&m_view);
  92. resizeView(360, 640);
  93. setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  94. }
  95. void MainWindow::resizeView(int width, int height)
  96. {
  97. if (m_panningEnabled)
  98. {
  99. m_pannableView->setMinimumSize(width, height);
  100. m_pannableView->setPreferredSize(width, height);
  101. m_pannableView->setMaximumSize(width, height);
  102. m_view.setFixedSize(width + 4, height + 4);
  103. setFixedSize(width + 4, height + menuBar()->size().height() + 4);
  104. m_hybridView->setMinimumSize(0, 0);
  105. }
  106. else
  107. {
  108. m_hybridView->resize(width, height);
  109. m_view.setFixedSize(width + 4, height + 4);
  110. setFixedSize(width + 4, height + menuBar()->size().height() + 4);
  111. }
  112. }
  113. void MainWindow::contentsSizeChanged(QSize newSize)
  114. {
  115. m_hybridView->setMinimumSize(newSize.width(), newSize.height());
  116. }
  117. void MainWindow::resizeW240H320()
  118. {
  119. resizeView(240, 320);
  120. }
  121. void MainWindow::resizeW320H240()
  122. {
  123. resizeView(320, 240);
  124. }
  125. void MainWindow::resizeW360H640()
  126. {
  127. resizeView(360, 640);
  128. }
  129. void MainWindow::resizeW640H360()
  130. {
  131. resizeView(640, 360);
  132. }
  133. void MainWindow::resizeW800H480()
  134. {
  135. resizeView(800, 480);
  136. }