Prechádzať zdrojové kódy

Create section from selection

Kandrashin Denis 12 rokov pred
rodič
commit
8235738c5e

+ 4 - 2
fb2edit.pro

@@ -54,7 +54,8 @@ OTHER_FILES += \
     source/js/get_status.js \
     source/js/get_location.js \
     source/js/insert_title.js \
-    CMakeLists.txt
+    CMakeLists.txt \
+    source/js/new_section.js
 
 if (unix) {
 
@@ -65,4 +66,5 @@ if (unix) {
 }
 
 FORMS += \
-    source/fb2find.ui
+    source/fb2find.ui \
+    source/fb2setup.ui

+ 12 - 0
source/fb2dlgs.cpp

@@ -4,6 +4,7 @@
 #include "fb2tree.hpp"
 #include "fb2utils.h"
 #include "ui_fb2find.h"
+#include "ui_fb2setup.h"
 
 #include <QComboBox>
 #include <QDialogButtonBox>
@@ -149,3 +150,14 @@ void FbNoteDlg::loadFinished()
     FbTextElement body = m_text->page()->mainFrame()->documentElement().findFirst("body");
     body.select();
 }
+
+//---------------------------------------------------------------------------
+//  FbSetupDlg
+//---------------------------------------------------------------------------
+
+FbSetupDlg::FbSetupDlg(QWidget *parent, Qt::WindowFlags f)
+    : QDialog(parent, f)
+    , ui(new Ui::FbSetup)
+{
+    ui->setupUi(this);
+}

+ 12 - 0
source/fb2dlgs.hpp

@@ -16,6 +16,7 @@ QT_END_NAMESPACE
 
 namespace Ui {
 class FbFind;
+class FbSetup;
 }
 
 class FbCodeFindDlg : public QDialog
@@ -67,4 +68,15 @@ private:
     QToolBar *m_toolbar;
 };
 
+class FbSetupDlg : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit FbSetupDlg(QWidget *parent = 0, Qt::WindowFlags f = 0);
+
+private:
+    Ui::FbSetup * ui;
+};
+
 #endif // FB2DLGS_H

+ 7 - 2
source/fb2main.cpp

@@ -5,6 +5,7 @@
 
 #include "fb2main.hpp"
 #include "fb2code.hpp"
+#include "fb2dlgs.hpp"
 #include "fb2read.hpp"
 #include "fb2save.hpp"
 #include "fb2text.hpp"
@@ -390,6 +391,9 @@ void FbMainWindow::createActions()
 
     menu->addSeparator();
 
+    actionSectSeparator = act = new QAction(tr("Section break"), this);
+    menu->addAction(act);
+
     actionParaSeparator = act = new QAction(tr("Paragraph"), this);
     menu->addAction(act);
 
@@ -504,8 +508,8 @@ void FbMainWindow::createActions()
 
 void FbMainWindow::openSettings()
 {
-    QMessageBox::about(this, tr("Settings"),
-        tr("The <b>fb2edit</b> is application for editing FB2-files."));
+    FbSetupDlg dlg(this);
+    dlg.exec();
 }
 
 void FbMainWindow::createTree()
@@ -705,6 +709,7 @@ void FbMainWindow::createTextToolbar()
     connect(actionDate, SIGNAL(triggered()), textPage, SLOT(insertDate()));
     connect(actionBody, SIGNAL(triggered()), textPage, SLOT(insertBody()));
 
+    connect(actionSectSeparator, SIGNAL(triggered()), textPage, SLOT(separateSection()));
     connect(actionParaSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertParagraphSeparator), SIGNAL(triggered()));
     connect(actionLineSeparator, SIGNAL(triggered()), textEdit->pageAction(QWebPage::InsertLineSeparator), SIGNAL(triggered()));
 

+ 1 - 0
source/fb2main.hpp

@@ -139,6 +139,7 @@ private:
         *actionStanza,
         *actionAuthor,
         *actionSection,
+        *actionSectSeparator,
         *actionParaSeparator,
         *actionLineSeparator,
         *actionClearFormat,

+ 107 - 0
source/fb2setup.ui

@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>FbSetup</class>
+ <widget class="QDialog" name="FbSetup">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Settings</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QTabWidget" name="tabWidget">
+     <property name="currentIndex">
+      <number>0</number>
+     </property>
+     <widget class="QWidget" name="tab">
+      <attribute name="title">
+       <string>Tab 1</string>
+      </attribute>
+      <layout class="QFormLayout" name="formLayout">
+       <item row="0" column="0">
+        <widget class="QLabel" name="label">
+         <property name="text">
+          <string>Default font:</string>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="1">
+        <widget class="QFontComboBox" name="fontComboBox"/>
+       </item>
+       <item row="1" column="0">
+        <widget class="QLabel" name="label_2">
+         <property name="text">
+          <string>Style file name:</string>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="1">
+        <widget class="QComboBox" name="comboBox">
+         <property name="editable">
+          <bool>true</bool>
+         </property>
+        </widget>
+       </item>
+      </layout>
+     </widget>
+     <widget class="QWidget" name="tab_2">
+      <attribute name="title">
+       <string>Tab 2</string>
+      </attribute>
+     </widget>
+    </widget>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>FbSetup</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>FbSetup</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

+ 6 - 0
source/fb2text.cpp

@@ -277,6 +277,12 @@ void FbTextPage::insertDate()
 {
 }
 
+void FbTextPage::separateSection()
+{
+    static const QString javascript = FB2::read(":/js/new_section.js");
+    mainFrame()->evaluateJavaScript(javascript);
+}
+
 FbTextElement FbTextPage::current()
 {
     return element(location());

+ 1 - 0
source/fb2text.hpp

@@ -90,6 +90,7 @@ public slots:
     void insertPoem();
     void insertStanza();
     void insertDate();
+    void separateSection();
 
 protected:
     virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);

+ 1 - 0
source/js/javascript.qrc

@@ -6,5 +6,6 @@
         <file>get_location.js</file>
         <file>set_cursor.js</file>
         <file>insert_title.js</file>
+        <file>new_section.js</file>
     </qresource>
 </RCC>

+ 26 - 0
source/js/new_section.js

@@ -0,0 +1,26 @@
+(function() {
+   if(window.getSelection().rangeCount){ 
+      var selection = window.getSelection()
+      range = selection.getRangeAt(0)
+      
+      start = range.startContainer
+      end = range.endContainer
+      root = range.commonAncestorContainer
+
+      if(start.nodeName.toLowerCase() == "body") return null
+      if(start.nodeName == "#text") start = start.parentNode
+      if(end.nodeName == "#text") end = end.parentNode
+
+      if(start == end) root = start
+
+      var range = document.createRange();
+      range.setStartBefore(start);
+      range.setEndAfter(end);
+
+      var newNode = document.createElement("div");
+      newNode.className = "section";
+      range.surroundContents(newNode);
+      
+      return newNode;
+    }
+})()