filebrowser/ui/src/editorview.cpp
changeset 17 4f2773374eff
child 31 e7a04a6385be
equal deleted inserted replaced
15:e11368ed4880 17:4f2773374eff
       
     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 #include <HbView>
       
    19 #include <HbMainWindow>
       
    20 #include <HbApplication>
       
    21 #include <HbAction>
       
    22 #include <HbMenu>
       
    23 #include <HbTextEdit>
       
    24 #include <hbpushbutton.h>
       
    25 
       
    26 #include <QGraphicsLinearLayout>
       
    27 #include <QFile>
       
    28 #include <QFileInfo>
       
    29 #include <QTextStream>
       
    30 #include <QByteArray>
       
    31 #include <QByteRef>
       
    32 
       
    33 #include "editorview.h"
       
    34 
       
    35 EditorView::EditorView(HbMainWindow &mainWindow)
       
    36     : mMainWindow(mainWindow),
       
    37     mTextEdit(0),
       
    38     mFileHex(false),
       
    39     mFlagReadOnly(),
       
    40     mFileName(),
       
    41     mFileContent(),
       
    42     mToTextAction(0),
       
    43     mToHexAction(0),
       
    44     mExitAction(0)
       
    45 {
       
    46     // Override back navigation action
       
    47     HbAction *backNaviAction = new HbAction(Hb::BackNaviAction, this);
       
    48     connect(backNaviAction, SIGNAL(triggered()), this, SLOT(backButtonClicked()));
       
    49     setNavigationAction(backNaviAction);
       
    50 
       
    51     createMenu();
       
    52 
       
    53     // text editor
       
    54 //    mTextEdit = new QTextEdit();
       
    55 //    QGraphicsLinearLayout *editorLayout = new QGraphicsLinearLayout(Qt::Vertical);
       
    56 //    QGraphicsProxyWidget *editorWidget = new QGraphicsProxyWidget();
       
    57 //    editorWidget->setWidget(mTextEdit);
       
    58 //    editorLayout->addItem(editorWidget);
       
    59 //    setLayout(editorLayout);
       
    60 
       
    61     mTextEdit = new HbTextEdit();
       
    62     mTextEdit->setAlignment(Qt::AlignTop);
       
    63     QGraphicsLinearLayout *editorLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
       
    64     editorLayout->addItem(mTextEdit);
       
    65     setLayout(editorLayout);
       
    66 }
       
    67 
       
    68 EditorView::~EditorView() 
       
    69 {
       
    70 //    delete mTextEdit;
       
    71 //    delete mToTextAction;
       
    72 //    delete mToHexAction;
       
    73 //    delete mExitAction;
       
    74 }
       
    75 
       
    76 void EditorView::open(const QString& fileName, bool flagReadOnly)
       
    77 {
       
    78     mFlagReadOnly = flagReadOnly;
       
    79 
       
    80     if (!fileName.isEmpty()) {
       
    81         mTextEdit->setReadOnly(mFlagReadOnly);
       
    82         loadFile(fileName);
       
    83     }
       
    84     else {
       
    85         // not valid file name
       
    86     }
       
    87 }
       
    88 
       
    89 void EditorView::loadFile(const QString &fileName)
       
    90 {
       
    91     mFileName = fileName;
       
    92     QFile file(fileName);
       
    93     QFileInfo baseName(fileName);
       
    94     setTitle(baseName.fileName());
       
    95 
       
    96     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
    97         // note: cannot be opened. readonly or not text
       
    98         return;
       
    99     }
       
   100     QTextStream in(&file);
       
   101     in.setAutoDetectUnicode(false);
       
   102 
       
   103     mFileContent = in.readAll();
       
   104     file.close();
       
   105     displayInText();
       
   106 }
       
   107 
       
   108 void EditorView::displayInText()
       
   109 {
       
   110     mFileHex = false;
       
   111     QByteArray asciiContent = mFileContent.toAscii();
       
   112     mTextEdit->setPlainText(QString(asciiContent));
       
   113 }
       
   114 
       
   115 void EditorView::displayInHex()
       
   116 {
       
   117     QByteArray asciiContent = mFileContent.toAscii().toHex();
       
   118     QString textInHex = QString(asciiContent);
       
   119     mTextEdit->setPlainText(textInHex);
       
   120     mFileHex = true;
       
   121 }
       
   122 
       
   123 void EditorView::createMenu()
       
   124 {
       
   125     mToTextAction = menu()->addAction("View as text");
       
   126     connect(mToTextAction, SIGNAL(triggered()), this, SLOT(displayInText()));
       
   127     mToHexAction = menu()->addAction("View as hex");
       
   128     connect(mToHexAction, SIGNAL(triggered()), this, SLOT(displayInHex()));
       
   129 //    mExitAction = menu()->addAction("Exit");
       
   130 //    connect(mExitAction, SIGNAL(triggered()), this, SLOT(backButtonClicked()));
       
   131 
       
   132     // update the menus before showing it:
       
   133     connect(menu(), SIGNAL(aboutToShow()), this, SLOT(updateMenu()));
       
   134 }
       
   135 
       
   136 void EditorView::updateMenu()
       
   137 {
       
   138     mToTextAction->setVisible(mFileHex);
       
   139     mToHexAction->setVisible(!mFileHex);
       
   140 }	
       
   141 
       
   142 void EditorView::backButtonClicked()
       
   143 {
       
   144     emit finished(false);
       
   145 }
       
   146