Kandrashin Denis 13 ani în urmă
părinte
comite
426d43f903
2 a modificat fișierele cu 32 adăugiri și 1 ștergeri
  1. 30 1
      source/fb2main.cpp
  2. 2 0
      source/fb2main.h

+ 30 - 1
source/fb2main.cpp

@@ -84,6 +84,34 @@ void MainWindow::logDestroyed()
     messageEdit = NULL;
 }
 
+static QTextFrame * findFrame(QTextFrame *root, const QModelIndex &index)
+{
+    if (!index.isValid()) return root;
+    if (QTextFrame *frame = findFrame(root, index.parent())) {
+        QTextFrame *child = static_cast<QTextFrame*>(index.internalPointer());
+        int i = frame->childFrames().indexOf(child);
+        if (i >= 0) return child;
+    }
+    return NULL;
+}
+
+void MainWindow::treeActivated(const QModelIndex &index)
+{
+    if (!treeView) return;
+    if (!textEdit) return;
+    if (!textEdit->document()) return;
+
+    QTextFrame * frame = textEdit->document()->rootFrame();
+    if (!frame) return;
+
+    QTextCursor cursor = textEdit->textCursor();
+    frame = findFrame(frame, index);
+    if (!frame) return;
+
+    cursor.setPosition(frame->firstPosition());
+    textEdit->setTextCursor(cursor);
+}
+
 void MainWindow::treeDestroyed()
 {
     treeView = NULL;
@@ -159,7 +187,8 @@ void MainWindow::sendDocument()
     treeView = new QTreeView(this);
     treeView->setModel(new Fb2TreeModel(*textEdit));
     treeView->setHeaderHidden(true);
-    connect(messageEdit, SIGNAL(destroyed()), SLOT(treeDestroyed()));
+    connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
+    connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
     QDockWidget * dock = new QDockWidget(tr("Contents"), this);
     dock->setAttribute(Qt::WA_DeleteOnClose);
     dock->setFeatures(QDockWidget::AllDockWidgetFeatures);

+ 2 - 0
source/fb2main.h

@@ -8,6 +8,7 @@ QT_BEGIN_NAMESPACE
 class QAction;
 class QMenu;
 class QFile;
+class QModelIndex;
 class QThread;
 class QTextEdit;
 class QTreeView;
@@ -42,6 +43,7 @@ private slots:
 
     void about();
     void documentWasModified();
+    void treeActivated(const QModelIndex &index);
     void treeDestroyed();
     void logDestroyed();
     void logShowed();