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