1
0
Kandrashin Denis 12 жил өмнө
parent
commit
c74aad6ef5

BIN
3rdparty/gnome/16x16/edit-clear.png


+ 32 - 23
source/fb2main.cpp

@@ -23,6 +23,21 @@ FbDockWidget::FbDockWidget(const QString &title, QWidget *parent, Qt::WindowFlag
     setAttribute(Qt::WA_DeleteOnClose);
 }
 
+//---------------------------------------------------------------------------
+//  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
 //---------------------------------------------------------------------------
@@ -61,6 +76,11 @@ FbMainWindow::FbMainWindow(const QString &filename, ViewMode mode)
     }
 }
 
+FbTextPage * FbMainWindow::page()
+{
+    return textFrame ? textFrame->view()->page() : 0;
+}
+
 void FbMainWindow::logMessage(const QString &message)
 {
     if (!messageEdit) {
@@ -378,30 +398,30 @@ void FbMainWindow::createActions()
 
     menuText = menu = menuBar()->addMenu(tr("Fo&rmat"));
 
-    actionClearFormat = act = new QAction(FbIcon("edit-clear"), tr("Clear format"), this);
+    actionClearFormat = act = new FbTextAction(FbIcon("edit-clear"), tr("Clear format"), QWebPage::RemoveFormat, this);
     menu->addAction(act);
 
     menu->addSeparator();
 
-    actionTextBold = act = new QAction(FbIcon("format-text-bold"), tr("&Bold"), this);
+    actionTextBold = act = new FbTextAction(FbIcon("format-text-bold"), tr("&Bold"), QWebPage::ToggleBold, this);
     act->setShortcuts(QKeySequence::Bold);
     act->setCheckable(true);
     menu->addAction(act);
 
-    actionTextItalic = act = new QAction(FbIcon("format-text-italic"), tr("&Italic"), this);
+    actionTextItalic = act = new FbTextAction(FbIcon("format-text-italic"), tr("&Italic"), QWebPage::ToggleItalic, this);
     act->setShortcuts(QKeySequence::Italic);
     act->setCheckable(true);
     menu->addAction(act);
 
-    actionTextStrike = act = new QAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), this);
+    actionTextStrike = act = new FbTextAction(FbIcon("format-text-strikethrough"), tr("&Strikethrough"), QWebPage::ToggleStrikethrough, this);
     act->setCheckable(true);
     menu->addAction(act);
 
-    actionTextSup = act = new QAction(FbIcon("format-text-superscript"), tr("Su&perscript"), this);
+    actionTextSup = act = new FbTextAction(FbIcon("format-text-superscript"), tr("Su&perscript"), QWebPage::ToggleSuperscript, this);
     act->setCheckable(true);
     menu->addAction(act);
 
-    actionTextSub = act = new QAction(FbIcon("format-text-subscript"), tr("Su&bscript"), this);
+    actionTextSub = act = new FbTextAction(FbIcon("format-text-subscript"), tr("Su&bscript"), QWebPage::ToggleSubscript, this);
     act->setCheckable(true);
     menu->addAction(act);
 
@@ -518,17 +538,6 @@ void FbMainWindow::selectionChanged()
     statusBar()->showMessage(view->page()->status());
 }
 
-void FbMainWindow::formatChanged()
-{
-    FbTextEdit *view = textFrame->view();
-    actionClearFormat->setEnabled(view->actionEnabled(QWebPage::RemoveFormat));
-    actionTextBold->setChecked(view->actionChecked(QWebPage::ToggleBold));
-    actionTextItalic->setChecked(view->actionChecked(QWebPage::ToggleItalic));
-    actionTextStrike->setChecked(view->actionChecked(QWebPage::ToggleStrikethrough));
-    actionTextSub->setChecked(view->actionChecked(QWebPage::ToggleSubscript));
-    actionTextSup->setChecked(view->actionChecked(QWebPage::ToggleSuperscript));
-}
-
 void FbMainWindow::canUndoChanged(bool canUndo)
 {
     actionUndo->setEnabled(canUndo);
@@ -673,12 +682,12 @@ void FbMainWindow::createTextToolbar()
     connect(actionTextSub, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(triggered()));
     connect(actionTextSup, SIGNAL(triggered()), textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(triggered()));
 
-    connect(textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(changed()), SLOT(formatChanged()));
-    connect(textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(changed()), SLOT(formatChanged()));
-    connect(textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(changed()), SLOT(formatChanged()));
-    connect(textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(changed()), SLOT(formatChanged()));
-    connect(textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(changed()), SLOT(formatChanged()));
-    connect(textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(changed()), SLOT(formatChanged()));
+    connect(textEdit->pageAction(QWebPage::RemoveFormat), SIGNAL(changed()), actionClearFormat, SLOT(updateEnabled()));
+    connect(textEdit->pageAction(QWebPage::ToggleBold), SIGNAL(changed()), actionTextBold, SLOT(updateChecked()));
+    connect(textEdit->pageAction(QWebPage::ToggleItalic), SIGNAL(changed()), actionTextItalic, SLOT(updateChecked()));
+    connect(textEdit->pageAction(QWebPage::ToggleStrikethrough), SIGNAL(changed()), actionTextStrike, SLOT(updateChecked()));
+    connect(textEdit->pageAction(QWebPage::ToggleSubscript), SIGNAL(changed()), actionTextSub, SLOT(updateChecked()));
+    connect(textEdit->pageAction(QWebPage::ToggleSuperscript), SIGNAL(changed()), actionTextSup, SLOT(updateChecked()));
 
     connect(actionFind, SIGNAL(triggered()), textEdit, SLOT(find()));
     connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));

+ 5 - 2
source/fb2main.hpp

@@ -17,8 +17,9 @@ QT_END_NAMESPACE
 class FbCodeEdit;
 class FbTreeView;
 class FbHeadView;
-class FbTextFrame;
 class FbTextEdit;
+class FbTextFrame;
+class FbTextPage;
 
 class FbDockWidget : public QDockWidget
 {
@@ -35,6 +36,9 @@ public:
     enum ViewMode { FB2, XML };
     explicit FbMainWindow(const QString &filename = QString(), ViewMode mode = FB2);
 
+public:
+    FbTextPage * page();
+
 protected:
     void closeEvent(QCloseEvent *event);
 
@@ -70,7 +74,6 @@ private slots:
     void status(const QString &text);
     void clipboardDataChanged();
     void selectionChanged();
-    void formatChanged();
     void undoChanged();
     void redoChanged();
     void openSettings();

+ 26 - 0
source/fb2text.cpp

@@ -726,3 +726,29 @@ void FbTextFrame::dockDestroyed()
     m_dock = 0;
 }
 
+//---------------------------------------------------------------------------
+//  FbTextAction
+//---------------------------------------------------------------------------
+
+FbTextAction::FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent)
+    : QAction(text, parent)
+    , m_action(action)
+{
+}
+
+FbTextAction::FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent)
+    : QAction(icon, text, parent)
+    , m_action(action)
+{
+}
+
+void FbTextAction::updateChecked()
+{
+    if (QAction * act = action(m_action)) setChecked(act->isChecked());
+}
+
+void FbTextAction::updateEnabled()
+{
+    if (QAction * act = action(m_action)) setEnabled(act->isEnabled());
+}
+

+ 21 - 1
source/fb2text.hpp

@@ -1,6 +1,7 @@
 #ifndef FB2TEXT_H
 #define FB2TEXT_H
 
+#include <QAction>
 #include <QFrame>
 #include <QResizeEvent>
 #include <QTimer>
@@ -168,7 +169,7 @@ public:
     ~FbTextFrame();
 
 public:
-    FbTextEdit *view() { return &m_view; }
+    FbTextEdit * view() { return &m_view; }
 
 public slots:
     void showInspector();
@@ -182,4 +183,23 @@ private:
     QDockWidget *m_dock;
 };
 
+class FbTextAction : public QAction
+{
+    Q_OBJECT
+
+public:
+    explicit FbTextAction(const QString &text, QWebPage::WebAction action, QObject* parent);
+    explicit FbTextAction(const QIcon &icon, const QString &text, QWebPage::WebAction action, QObject* parent);
+
+public slots:
+    void updateChecked();
+    void updateEnabled();
+
+private:
+    QAction * action(QWebPage::WebAction action);
+
+private:
+    QWebPage::WebAction m_action;
+};
+
 #endif // FB2TEXT_H