Browse Source

Switch edit mode

Kandrashin Denis 12 years ago
parent
commit
ee09a33cf1
8 changed files with 86 additions and 86 deletions
  1. 34 14
      source/fb2dock.cpp
  2. 22 3
      source/fb2dock.hpp
  3. 7 0
      source/fb2enum.h
  4. 15 45
      source/fb2main.cpp
  5. 1 16
      source/fb2main.hpp
  6. 6 4
      source/fb2text.cpp
  7. 1 1
      source/fb2text.hpp
  8. 0 3
      source/fb2tree.hpp

+ 34 - 14
source/fb2dock.cpp

@@ -6,6 +6,24 @@
 #include <QLayout>
 #include <QtDebug>
 
+//---------------------------------------------------------------------------
+//  FbModeAction
+//---------------------------------------------------------------------------
+
+FbModeAction::FbModeAction(FbMainDock *parent, Fb::Mode mode, const QString &text)
+    : QAction(text, parent)
+    , m_dock(parent)
+    , m_mode(mode)
+{
+    setCheckable(true);
+    connect(this, SIGNAL(triggered()), SLOT(switchMode()));
+}
+
+void FbModeAction::switchMode()
+{
+    m_dock->setMode(m_mode);
+}
+
 //---------------------------------------------------------------------------
 //  FbMainDock
 //---------------------------------------------------------------------------
@@ -28,21 +46,14 @@ FbMainDock::FbMainDock(QWidget *parent)
     m_head->setText(m_text);
 }
 
-FbMainDock::Mode FbMainDock::mode() const
+void FbMainDock::setMode(Fb::Mode mode)
 {
-    QWidget * current = currentWidget();
-    if (current == textFrame) return Text;
-    if (current == m_head) return Head;
-    if (current == m_code) return Code;
-    return Text;
-}
-
-void FbMainDock::setMode(Mode mode)
-{
-    switch (mode) {
-        case Text: setCurrentWidget(textFrame); return;
-        case Head: setCurrentWidget(m_head); return;
-        case Code: setCurrentWidget(m_code); return;
+    if (mode == m_mode) return;
+    switch (m_mode = mode) {
+        case Fb::Text: setCurrentWidget(textFrame); break;
+        case Fb::Head: setCurrentWidget(m_head); break;
+        case Fb::Code: setCurrentWidget(m_code); break;
+        case Fb::Html: setCurrentWidget(m_code); break;
     }
 }
 
@@ -74,3 +85,12 @@ bool FbMainDock::save(QIODevice *device, const QString &codec)
     return true;
 }
 
+bool FbMainDock::isModified() const
+{
+    if (isSwitched) return true;
+    QWidget * current = currentWidget();
+    if (current == textFrame) return m_text->isModified();
+    if (current == m_head) return m_text->isModified();
+    if (current == m_code) return m_code->isModified();
+    return false;
+}

+ 22 - 3
source/fb2dock.hpp

@@ -7,25 +7,43 @@
 #include <QToolBar>
 #include <QIODevice>
 
+#include "fb2enum.h"
+
 class FbTextEdit;
 class FbHeadEdit;
 class FbCodeEdit;
 
+class FbMainDock;
+
+class FbModeAction : public QAction
+{
+    Q_OBJECT
+public:
+    explicit FbModeAction(FbMainDock *parent, Fb::Mode mode, const QString &text);
+
+private slots:
+    void switchMode();
+
+private:
+    FbMainDock * m_dock;
+    Fb::Mode m_mode;
+};
+
 class FbMainDock : public QStackedWidget
 {
     Q_OBJECT
 
 public:
-    enum Mode { Text = 0, Head, Code };
     explicit FbMainDock(QWidget *parent = 0);
     FbTextEdit * text() { return m_text; }
     FbHeadEdit * head() { return m_head; }
     FbCodeEdit * code() { return m_code; }
     bool load(const QString &filename);
     bool save(QIODevice *device, const QString &codec = QString());
-    Mode mode() const;
-    void setMode(Mode mode);
+    Fb::Mode mode() const { return m_mode; }
+    void setMode(Fb::Mode mode);
     void setTool(QToolBar *tool) { m_tool = tool; }
+    bool isModified() const;
 
 signals:
     
@@ -38,6 +56,7 @@ private:
     FbCodeEdit *m_code;
     QToolBar *m_tool;
     bool isSwitched;
+    Fb::Mode m_mode;
 };
 
 #endif // FB2DOCK_H

+ 7 - 0
source/fb2enum.h

@@ -3,6 +3,13 @@
 
 namespace Fb {
 
+enum Mode {
+    Text,
+    Head,
+    Code,
+    Html,
+};
+
 enum Actions {
     GoBack,
     GoForward,

+ 15 - 45
source/fb2main.cpp

@@ -14,21 +14,6 @@
 #include "fb2text.hpp"
 #include "fb2utils.h"
 
-//---------------------------------------------------------------------------
-//  FbTextAction
-//---------------------------------------------------------------------------
-
-QAction * FbTextAction::action(QWebPage::WebAction action)
-{
-    FbMainWindow * main = qobject_cast<FbMainWindow*>(parent());
-    if (!main) return 0;
-
-    FbTextPage * page = main->page();
-    if (!page) return 0;
-
-    return page->action(action);
-}
-
 //---------------------------------------------------------------------------
 //  FbMainWindow
 //---------------------------------------------------------------------------
@@ -162,11 +147,6 @@ void FbMainWindow::documentWasModified()
 //    setModified(isSwitched || codeEdit->isModified());
 }
 
-void FbMainWindow::cleanChanged(bool clean)
-{
-//    setModified(isSwitched || !clean);
-}
-
 void FbMainWindow::setModified(bool modified)
 {
     QFileInfo info = windowFilePath();
@@ -235,11 +215,6 @@ void FbMainWindow::createActions()
 
     menu = menuBar()->addMenu(tr("&Edit"));
 
-    tool = addToolBar(tr("Edit"));
-    tool->setMovable(false);
-    tool->addSeparator();
-    mainDock->setTool(tool);
-
     connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
 
     act = new QAction(FbIcon("edit-undo"), tr("&Undo"), this);
@@ -450,32 +425,24 @@ void FbMainWindow::createActions()
 
     QActionGroup * viewGroup = new QActionGroup(this);
 
-    act = new QAction(tr("&Text"), this);
-    act->setCheckable(true);
-    act->setChecked(true);
-    connect(act, SIGNAL(triggered()), this, SLOT(viewText()));
+    act = new FbModeAction(mainDock, Fb::Text, tr("&Text"));
     viewGroup->addAction(act);
     menu->addAction(act);
     tool->addAction(act);
+    act->setChecked(true);
 
-    act = new QAction(tr("&Head"), this);
-    act->setCheckable(true);
-    connect(act, SIGNAL(triggered()), this, SLOT(viewHead()));
+    act = new FbModeAction(mainDock, Fb::Head, tr("&Head"));
     viewGroup->addAction(act);
     menu->addAction(act);
     tool->addAction(act);
 
-    act = new QAction(tr("&XML"), this);
-    act->setCheckable(true);
-    connect(act, SIGNAL(triggered()), this, SLOT(viewCode()));
+    act = new FbModeAction(mainDock, Fb::Code, tr("&XML"));
     viewGroup->addAction(act);
     menu->addAction(act);
     tool->addAction(act);
 
 #ifdef QT_DEBUG
-    act = new QAction(tr("&HTML"), this);
-    act->setCheckable(true);
-    connect(act, SIGNAL(triggered()), this, SLOT(viewHtml()));
+    act = new FbModeAction(mainDock, Fb::Html, tr("&HTML"));
     viewGroup->addAction(act);
     menu->addAction(act);
 #endif // _DEBUG
@@ -527,6 +494,11 @@ void FbMainWindow::createActions()
     act->setStatusTip(tr("Show the Qt library's About box"));
     connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
     menu->addAction(act);
+
+    toolEdit = tool = addToolBar(tr("Edit"));
+    tool->setMovable(false);
+    tool->addSeparator();
+    mainDock->setTool(tool);
 }
 
 void FbMainWindow::openSettings()
@@ -558,8 +530,7 @@ void FbMainWindow::writeSettings()
 
 bool FbMainWindow::maybeSave()
 {
-/*
-    if (textFrame && textFrame->view()->isModified()) {
+    if (mainDock->isModified()) {
         QMessageBox::StandardButton ret;
         ret = QMessageBox::warning(this, qApp->applicationName(),
                      tr("The document has been modified. Do you want to save your changes?"),
@@ -571,7 +542,6 @@ bool FbMainWindow::maybeSave()
             return false;
     }
     return true;
-*/  return false;
 }
 
 bool FbMainWindow::saveFile(const QString &fileName, const QString &codec)
@@ -870,15 +840,15 @@ void FbMainWindow::viewImgs()
 {
     if (dockImgs) dockImgs->deleteLater(); else createImgs();
 }
-
+*/
 void FbMainWindow::clipboardDataChanged()
 {
     if (const QMimeData *md = QApplication::clipboard()->mimeData()) {
-        actionPaste->setEnabled(md->hasText());
-        actionPasteText->setEnabled(md->hasText());
+//        actionPaste->setEnabled(md->hasText());
+//        actionPasteText->setEnabled(md->hasText());
     }
 }
-*/
+
 void FbMainWindow::status(const QString &text)
 {
     statusBar()->showMessage(text);

+ 1 - 16
source/fb2main.hpp

@@ -45,9 +45,6 @@ public:
     enum ViewMode { FB2, XML };
     explicit FbMainWindow(const QString &filename = QString(), ViewMode mode = FB2);
 
-public:
-    FbTextPage * page();
-
 protected:
     void closeEvent(QCloseEvent *event);
 
@@ -64,24 +61,12 @@ private slots:
     bool fileSaveAs();
 
     void about();
-    void createTextToolbar();
     void documentWasModified();
-    void checkScintillaUndo();
     void logDestroyed();
     void logShowed();
-    void viewCode();
-    void viewHtml();
-    void viewText(FbTextPage *page = 0);
-    void viewHead();
-
-    void cleanChanged(bool clean);
-    void canUndoChanged(bool canUndo);
-    void canRedoChanged(bool canRedo);
+
     void status(const QString &text);
     void clipboardDataChanged();
-    void selectionChanged();
-    void undoChanged();
-    void redoChanged();
     void openSettings();
 
 private:

+ 6 - 4
source/fb2text.cpp

@@ -22,6 +22,7 @@
 
 #include "fb2dlgs.hpp"
 #include "fb2page.hpp"
+#include "fb2save.hpp"
 #include "fb2tree.hpp"
 #include "fb2utils.h"
 
@@ -162,7 +163,7 @@ void FbTextBase::addTools(QToolBar *tool)
 //  FbTextEdit
 //---------------------------------------------------------------------------
 
-FbTextEdit::FbTextEdit(QWidget *parent, QWidget *owner)
+FbTextEdit::FbTextEdit(QWidget *parent, QObject *owner)
     : FbTextBase(parent)
     , m_owner(qobject_cast<QMainWindow*>(owner))
     , m_noteView(0)
@@ -280,6 +281,7 @@ void FbTextEdit::disconnectActions()
 {
     foreach (QAction *action, m_actions) {
         action->setDisabled(true);
+        action->setChecked(false);
         disconnect(action);
     }
 }
@@ -563,7 +565,7 @@ FbWebFrame::FbWebFrame(QWidget *parent)
 
 FbTextFrame::FbTextFrame(QWidget *parent, QAction *action)
     : QFrame(parent)
-    , m_view(this)
+    , m_view(this, parent->parent())
     , m_dock(0)
 {
     setFrameShape(QFrame::StyledPanel);
@@ -632,11 +634,11 @@ FbTextAction::FbTextAction(const QIcon &icon, const QString &text, QWebPage::Web
 
 void FbTextAction::updateChecked()
 {
-    if (QAction * act = action(m_action)) setChecked(act->isChecked());
+//    if (QAction * act = action(m_action)) setChecked(act->isChecked());
 }
 
 void FbTextAction::updateEnabled()
 {
-    if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
+//    if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
 }
 

+ 1 - 1
source/fb2text.hpp

@@ -73,7 +73,7 @@ class FbTextEdit : public FbTextBase
     Q_OBJECT
 
 public:
-    explicit FbTextEdit(QWidget *parent, QWidget *owner);
+    explicit FbTextEdit(QWidget *parent, QObject *owner);
     virtual ~FbTextEdit();
 
     FbTextPage *page();

+ 0 - 3
source/fb2tree.hpp

@@ -162,9 +162,6 @@ private slots:
     void moveLeft();
     void moveRight();
 
-    void treeDestroyed();
-    void imgsDestroyed();
-
 protected:
     void keyPressEvent(QKeyEvent *event);