Navigation Menu

Skip to content

Commit

Permalink
implemented hscrolling
Browse files Browse the repository at this point in the history
had to subclass `QWebView` for that. i don't like c++/qt's subclass-to-
class in the designer.
  • Loading branch information
sschober committed May 25, 2012
1 parent 465ff64 commit 5e1baf3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
14 changes: 14 additions & 0 deletions hscrollwebview.cpp
@@ -0,0 +1,14 @@
#include "hscrollwebview.h"
#include <QDebug>
#include <QWheelEvent>
#include <QWebPage>
#include <QWebFrame>

HScrollWebView::HScrollWebView(QWidget *parent) :
QWebView(parent)
{
}

void HScrollWebView::wheelEvent(QWheelEvent *ev){
page()->currentFrame()->scroll(-1 * ev->delta(),0);
}
21 changes: 21 additions & 0 deletions hscrollwebview.h
@@ -0,0 +1,21 @@
#ifndef HSCROLLWEBVIEW_H
#define HSCROLLWEBVIEW_H

#include <QWebView>

class HScrollWebView : public QWebView
{
Q_OBJECT
public:
explicit HScrollWebView(QWidget *parent = 0);

protected:
virtual void wheelEvent(QWheelEvent *ev);

signals:

public slots:

};

#endif // HSCROLLWEBVIEW_H
10 changes: 7 additions & 3 deletions mainwindow.cpp
Expand Up @@ -18,6 +18,9 @@ MainWindow::MainWindow(QWidget *parent) :
{
ui->setupUi(this);

hswv = new HScrollWebView(this);
ui->splitter->addWidget(hswv);

QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
Expand All @@ -39,6 +42,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionDirectory, SIGNAL(triggered()),this,SLOT(viewDirectory()));
connect(ui->listView, SIGNAL(clicked(QModelIndex)), this, SLOT(dirViewClicked(QModelIndex)));


ui->actionSave->setEnabled(false);
ui->sourceView->hide();

Expand Down Expand Up @@ -187,9 +191,9 @@ void MainWindow::textChanged(){
t.start();
QString newText = wrapInHTMLDoc(markdown(ui->plainTextEdit->toPlainText()));
ui->statusBar->showMessage(QString("Render time: %1 ms").arg(t.elapsed()));
QPoint pos = ui->webView->page()->currentFrame()->scrollPosition();
ui->webView->setHtml(newText);
ui->webView->page()->currentFrame()->setScrollPosition(pos);
QPoint pos = hswv->page()->currentFrame()->scrollPosition();
hswv->setHtml(newText);
ui->sourceView->setPlainText(newText);
hswv->page()->currentFrame()->setScrollPosition(pos);
}

4 changes: 4 additions & 0 deletions mainwindow.h
Expand Up @@ -5,6 +5,8 @@
#include <QFile>
#include <QModelIndex>

#include "hscrollwebview.h"

namespace Ui {
class MainWindow;
}
Expand Down Expand Up @@ -34,6 +36,8 @@ public slots:

Ui::MainWindow *ui;
QFile *currentFile;

HScrollWebView *hswv;
};

#endif // MAINWINDOW_H
14 changes: 0 additions & 14 deletions mainwindow.ui
Expand Up @@ -35,13 +35,6 @@
</size>
</property>
</widget>
<widget class="QWebView" name="webView">
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
<widget class="QPlainTextEdit" name="sourceView"/>
</widget>
</item>
Expand Down Expand Up @@ -111,13 +104,6 @@
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QWebView</class>
<extends>QWidget</extends>
<header>QtWebKit/QWebView</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
6 changes: 4 additions & 2 deletions qarkdown.pro
Expand Up @@ -11,9 +11,11 @@ TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp
mainwindow.cpp \
hscrollwebview.cpp

HEADERS += mainwindow.h
HEADERS += mainwindow.h \
hscrollwebview.h

FORMS += mainwindow.ui

Expand Down

0 comments on commit 5e1baf3

Please sign in to comment.