Explorar el Código

Select text frame from QTreeView

Kandrashin Denis hace 13 años
padre
commit
e4cfac1c4c
Se han modificado 3 ficheros con 21 adiciones y 9 borrados
  1. 2 9
      source/fb2main.cpp
  2. 14 0
      source/fb2tree.cpp
  3. 5 0
      source/fb2tree.h

+ 2 - 9
source/fb2main.cpp

@@ -101,15 +101,8 @@ void MainWindow::treeActivated(const QModelIndex &index)
     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);
+    Fb2TreeModel *model = static_cast<Fb2TreeModel*>(treeView->model());
+    model->select(index);
 }
 
 void MainWindow::treeDestroyed()

+ 14 - 0
source/fb2tree.cpp

@@ -100,3 +100,17 @@ QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
     Fb2TreeItem * i  = item(index);
     return i ? i->text() : QVariant();
 }
+
+void Fb2TreeModel::select(const QModelIndex &index)
+{
+    Fb2TreeItem * i = item(index);
+    if (!i) return;
+
+    QTextFrame * f = i->frame();
+    if (!f) return;
+
+    QTextCursor cursor = m_text.textCursor();
+    cursor.setPosition(f->firstPosition());
+    m_text.moveCursor(QTextCursor::End);
+    m_text.setTextCursor(cursor);
+}

+ 5 - 0
source/fb2tree.h

@@ -27,6 +27,10 @@ public:
         return m_parent;
     }
 
+    QTextFrame * frame() const {
+        return m_frame;
+    }
+
     QString text() const;
 
 private:
@@ -43,6 +47,7 @@ class Fb2TreeModel: public QAbstractItemModel
 public:
     explicit Fb2TreeModel(QTextEdit &text, QObject *parent = 0);
     virtual ~Fb2TreeModel();
+    void select(const QModelIndex &index);
 
 public:
     virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;