ginebra2/ContentViews/WindowsView.cpp
author hgs
Fri, 15 Oct 2010 17:30:59 -0400
changeset 16 3c88a81ff781
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
hgs
parents:
diff changeset
     1
#include "WindowsView.h"
hgs
parents:
diff changeset
     2
#include "BrowserWindow.h"
hgs
parents:
diff changeset
     3
#include "GWebContentView.h"
hgs
parents:
diff changeset
     4
#include "ViewStack.h"
hgs
parents:
diff changeset
     5
hgs
parents:
diff changeset
     6
//For page created signal
hgs
parents:
diff changeset
     7
#include "wrtbrowsercontainer.h"
hgs
parents:
diff changeset
     8
#include "webpagecontroller.h"
hgs
parents:
diff changeset
     9
hgs
parents:
diff changeset
    10
//For shared chrome object
hgs
parents:
diff changeset
    11
#include "ChromeWidget.h"
hgs
parents:
diff changeset
    12
#include "ChromeView.h"
hgs
parents:
diff changeset
    13
hgs
parents:
diff changeset
    14
#include <QDebug>
hgs
parents:
diff changeset
    15
hgs
parents:
diff changeset
    16
namespace GVA {
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
  WindowsView::WindowsView(ChromeWidget * chrome, QObject * parent, const QString & objectName)
hgs
parents:
diff changeset
    19
    : ControllableViewBase(parent),
hgs
parents:
diff changeset
    20
      m_current(0),
hgs
parents:
diff changeset
    21
      m_chrome(chrome),
hgs
parents:
diff changeset
    22
      m_menu(0)
hgs
parents:
diff changeset
    23
{
hgs
parents:
diff changeset
    24
  setObjectName(objectName);
hgs
parents:
diff changeset
    25
}
hgs
parents:
diff changeset
    26
hgs
parents:
diff changeset
    27
WindowsView::~WindowsView()
hgs
parents:
diff changeset
    28
{
hgs
parents:
diff changeset
    29
  foreach (QMainWindow * window, m_windows){
hgs
parents:
diff changeset
    30
    delete window;
hgs
parents:
diff changeset
    31
  }   
hgs
parents:
diff changeset
    32
}
hgs
parents:
diff changeset
    33
hgs
parents:
diff changeset
    34
void WindowsView::activate(BrowserWindow * window)
hgs
parents:
diff changeset
    35
{
hgs
parents:
diff changeset
    36
hgs
parents:
diff changeset
    37
  ViewStack * viewStack = ViewStack::getSingleton();
hgs
parents:
diff changeset
    38
  GWebContentView* webView = contentView();
hgs
parents:
diff changeset
    39
  if(webView) {
hgs
parents:
diff changeset
    40
    if(contentView()->currentPageIsSuperPage()){
hgs
parents:
diff changeset
    41
      webView->showNormalPage();
hgs
parents:
diff changeset
    42
      webView->setGesturesEnabled(true);
hgs
parents:
diff changeset
    43
      window->setView("WebView"); 
hgs
parents:
diff changeset
    44
      viewStack->switchView(window->view(), viewStack->getViewController()->currentViewName());
hgs
parents:
diff changeset
    45
    }
hgs
parents:
diff changeset
    46
    else {
hgs
parents:
diff changeset
    47
      viewStack->getViewController()->currentView()->deactivate();
hgs
parents:
diff changeset
    48
    }
hgs
parents:
diff changeset
    49
    window->setTitle(viewStack->getViewController()->currentView()->title());
hgs
parents:
diff changeset
    50
  }
hgs
parents:
diff changeset
    51
  // Let the chrome take focus, or whatever it wants to do.
hgs
parents:
diff changeset
    52
  m_chrome->activate();
hgs
parents:
diff changeset
    53
}
hgs
parents:
diff changeset
    54
hgs
parents:
diff changeset
    55
void WindowsView::deactivate(BrowserWindow * window)
hgs
parents:
diff changeset
    56
{
hgs
parents:
diff changeset
    57
  ViewStack * viewStack = ViewStack::getSingleton();
hgs
parents:
diff changeset
    58
  /*
hgs
parents:
diff changeset
    59
  if(m_current != window){
hgs
parents:
diff changeset
    60
    m_current = window;
hgs
parents:
diff changeset
    61
    viewStack->switchView(window->view(), viewStack->getViewController()->currentViewName());
hgs
parents:
diff changeset
    62
  } 
hgs
parents:
diff changeset
    63
  else */
hgs
parents:
diff changeset
    64
  //#ifndef BEDROCK_TILED_BACKING_STORE
hgs
parents:
diff changeset
    65
  if(viewStack->getViewController()->currentView())
hgs
parents:
diff changeset
    66
    viewStack->getViewController()->currentView()->activate();
hgs
parents:
diff changeset
    67
  //#endif
hgs
parents:
diff changeset
    68
}
hgs
parents:
diff changeset
    69
hgs
parents:
diff changeset
    70
hgs
parents:
diff changeset
    71
void WindowsView::setMenu(QMenu * menu)
hgs
parents:
diff changeset
    72
{
hgs
parents:
diff changeset
    73
  m_menu = menu;
hgs
parents:
diff changeset
    74
  if(m_menu){
hgs
parents:
diff changeset
    75
    m_menu->addAction("New Window", this, SLOT(onWindowAction()));
hgs
parents:
diff changeset
    76
  }
hgs
parents:
diff changeset
    77
}
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
void WindowsView::setMenuEnabled(bool enabled)
hgs
parents:
diff changeset
    80
{
hgs
parents:
diff changeset
    81
  m_menu->setEnabled(enabled);
hgs
parents:
diff changeset
    82
  m_current->setMenuEnabled(enabled);
hgs
parents:
diff changeset
    83
}
hgs
parents:
diff changeset
    84
hgs
parents:
diff changeset
    85
bool WindowsView::isMenuEnabled() const {
hgs
parents:
diff changeset
    86
    return m_menu->isEnabled();
hgs
parents:
diff changeset
    87
}
hgs
parents:
diff changeset
    88
hgs
parents:
diff changeset
    89
void WindowsView::handlePageEvents(bool handleEvents){
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
  if(handleEvents){
hgs
parents:
diff changeset
    92
    connect(WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer *)),
hgs
parents:
diff changeset
    93
	  this, SLOT(onPageCreated(WRT::WrtBrowserContainer *)));
hgs
parents:
diff changeset
    94
  }else{
hgs
parents:
diff changeset
    95
    disconnect(WebPageController::getSingleton(), SIGNAL(pageCreated(WRT::WrtBrowserContainer *)),
hgs
parents:
diff changeset
    96
	  this, SLOT(onPageCreated(WRT::WrtBrowserContainer *)));
hgs
parents:
diff changeset
    97
  }
hgs
parents:
diff changeset
    98
}
hgs
parents:
diff changeset
    99
hgs
parents:
diff changeset
   100
BrowserWindow * WindowsView::addWindow(WRT::WrtBrowserContainer * page)
hgs
parents:
diff changeset
   101
{
hgs
parents:
diff changeset
   102
  BrowserWindow * window = new BrowserWindow(m_chrome, this);
hgs
parents:
diff changeset
   103
  window->setPage(page);
hgs
parents:
diff changeset
   104
  window->menuBar()->addMenu(m_menu);
hgs
parents:
diff changeset
   105
  m_windows[page] = window;
hgs
parents:
diff changeset
   106
  m_current = window;
hgs
parents:
diff changeset
   107
  m_current->show();
hgs
parents:
diff changeset
   108
  deactivate(window);
hgs
parents:
diff changeset
   109
  return window;
hgs
parents:
diff changeset
   110
}
hgs
parents:
diff changeset
   111
hgs
parents:
diff changeset
   112
GWebContentView * WindowsView::contentView()
hgs
parents:
diff changeset
   113
{
hgs
parents:
diff changeset
   114
  return static_cast<GWebContentView*>(ViewStack::getSingleton()->getViewController()->view("WebView"));
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
void WindowsView::onWindowAction() 
hgs
parents:
diff changeset
   118
{
hgs
parents:
diff changeset
   119
  //Tell the page controller to create a new QWebPage. 
hgs
parents:
diff changeset
   120
  //The controller emits pageCreated when this happens
hgs
parents:
diff changeset
   121
  //The onPageCreated slot actually creates the window
hgs
parents:
diff changeset
   122
  WebPageController::getSingleton()->openPage();
hgs
parents:
diff changeset
   123
}
hgs
parents:
diff changeset
   124
  
hgs
parents:
diff changeset
   125
void WindowsView::onPageCreated(WRT::WrtBrowserContainer * page)
hgs
parents:
diff changeset
   126
{
hgs
parents:
diff changeset
   127
  addWindow(page);
hgs
parents:
diff changeset
   128
  //WebPageController::getSingleton()->loadFromHistory();
hgs
parents:
diff changeset
   129
}
hgs
parents:
diff changeset
   130
hgs
parents:
diff changeset
   131
void WindowsView::onPageDeleted(WRT::WrtBrowserContainer * page)
hgs
parents:
diff changeset
   132
{
hgs
parents:
diff changeset
   133
  assert(m_windows.contains(page));
hgs
parents:
diff changeset
   134
  delete m_windows.take(page);
hgs
parents:
diff changeset
   135
}
hgs
parents:
diff changeset
   136
hgs
parents:
diff changeset
   137
};