ginebra/emulator/chromeconsole.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "chromeconsole.h"
       
    20 #include "../chromeview.h"
       
    21 #include "../chromewidget.h"
       
    22 #include "qwebpage.h"
       
    23 #include "qwebframe.h"
       
    24 #include "ui_console.h"
       
    25 
       
    26 ChromeConsole::ChromeConsole(ChromeView *chromeView)
       
    27   : QDialog(),
       
    28     m_chromeView(chromeView),
       
    29     m_historyIndex(0)
       
    30 {
       
    31     m_ui = new Ui_ConsoleDialog;
       
    32     m_ui->setupUi(this);
       
    33 
       
    34     QList<int> sizes;
       
    35     sizes.append(300);
       
    36     sizes.append(30);
       
    37     m_ui->splitter->setSizes(sizes);
       
    38 
       
    39     // Read the saved state from disk.
       
    40     try {
       
    41         QFile file("console.dat");
       
    42         if(file.open(QIODevice::ReadOnly)) {
       
    43             QDataStream in(&file);
       
    44             QString str;
       
    45             in >> str;
       
    46             m_ui->outputEdit->setPlainText(str);
       
    47             in >> str;
       
    48             m_ui->inputEdit->setPlainText(str);
       
    49             int i;
       
    50             in >> i;
       
    51             QTextCursor cursor = m_ui->inputEdit->textCursor();
       
    52             cursor.setPosition(i);
       
    53             m_ui->inputEdit->setTextCursor(cursor);
       
    54             in >> m_expressionHistory;
       
    55             file.close();
       
    56         }
       
    57     }
       
    58     catch(...) {
       
    59     }
       
    60     m_ui->outputEdit->moveCursor(QTextCursor::End);
       
    61     m_ui->outputEdit->ensureCursorVisible();
       
    62 }
       
    63 
       
    64 QVariant ChromeConsole::evaluateExpression(const QString &expression) {
       
    65     m_expressionHistory.push_front(expression);
       
    66     m_historyIndex = 0;
       
    67 
       
    68     QVariant result = m_chromeView->getChromeWidget()->chromePage()->mainFrame()->evaluateJavaScript(expression);
       
    69     qDebug() << result;
       
    70     return result;
       
    71 }
       
    72 
       
    73 void ChromeConsole::evaluate() {   // slot
       
    74     QString expression = m_ui->inputEdit->toPlainText();
       
    75     m_ui->outputEdit->appendPlainText(">> " + expression);
       
    76     QVariant result = evaluateExpression(expression);
       
    77 
       
    78     // Figure out the best way to print the result, must be a better way to do this...
       
    79     if(result == QVariant()) {
       
    80         m_ui->outputEdit->appendPlainText("undefined");
       
    81     }
       
    82     else if(result.toString() != "") {
       
    83         m_ui->outputEdit->appendPlainText(result.toString());
       
    84     }
       
    85     else if(result.type() == QVariant::List) {
       
    86         m_ui->outputEdit->appendPlainText(result.toStringList().join(","));
       
    87     }
       
    88     else {
       
    89         // Probably a complex object, this won't be a real javascript value but it
       
    90         // will be informative to the developer.
       
    91         //m_ui->outputEdit->appendPlainText(result.typeName());
       
    92         QString resultString;
       
    93         QDebug debug(&resultString);
       
    94         debug << result;
       
    95         m_ui->outputEdit->appendPlainText(resultString);
       
    96     }
       
    97 
       
    98     m_ui->outputEdit->ensureCursorVisible();
       
    99 
       
   100     // Force outputEdit to repaint, otherwise get garbled text (at least on Windows).
       
   101     // Note: m_ui->outputEdit->update() doesn't work...
       
   102     m_ui->outputEdit->hide();
       
   103     m_ui->outputEdit->show();
       
   104 }
       
   105 
       
   106 void ChromeConsole::keyPressEvent(QKeyEvent *event) {
       
   107     switch(event->key()) {
       
   108         case Qt::Key_Enter:
       
   109         case Qt::Key_Return:
       
   110           if(event->modifiers() | Qt::ControlModifier)
       
   111               evaluate();
       
   112           break;
       
   113         case Qt::Key_Up:
       
   114           if(event->modifiers() | Qt::ControlModifier) {
       
   115               if(m_historyIndex < m_expressionHistory.count() - 1)
       
   116                   m_ui->inputEdit->setPlainText(m_expressionHistory[++m_historyIndex]);
       
   117           }
       
   118           break;
       
   119         case Qt::Key_Down:
       
   120           if(event->modifiers() | Qt::ControlModifier) {
       
   121               if(m_historyIndex > 0)
       
   122                   m_ui->inputEdit->setPlainText(m_expressionHistory[--m_historyIndex]);
       
   123           }
       
   124           break;
       
   125     }
       
   126 }
       
   127 
       
   128 void ChromeConsole::accept() {  // slot
       
   129     try {
       
   130         // Save the state to disk.
       
   131         QFile file("console.dat");
       
   132         if(file.open(QIODevice::WriteOnly)) {
       
   133             QDataStream out(&file);
       
   134             out << m_ui->outputEdit->toPlainText();
       
   135             out << m_ui->inputEdit->toPlainText();
       
   136             out << m_ui->inputEdit->textCursor().position();
       
   137             out << m_expressionHistory;
       
   138             file.close();
       
   139         }
       
   140     }
       
   141     catch(...) {
       
   142     }
       
   143     QDialog::accept();
       
   144 }