Commit 1f38382f authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[T12843][GSoC 2020] Add zoom widget

parent b2d5ef8d
Pipeline #21407 passed with stage
in 19 minutes and 54 seconds
......@@ -22,6 +22,8 @@
#include <config-cantor.h>
#include <array>
#include "cantor_part.h"
#include "lib/assistant.h"
#include "lib/backend.h"
......@@ -45,6 +47,7 @@
#include <KRun>
#include <KStandardAction>
#include <KToggleAction>
#include <KSelectAction>
#include <KXMLGUIFactory>
#include <KZip>
......@@ -59,7 +62,8 @@
#include <QTextStream>
#include <QTextEdit>
#include <QTimer>
#include <QRegularExpression>
#include <QComboBox>
//A concrete implementation of the WorksheetAccesssInterface
class WorksheetAccessInterfaceImpl : public Cantor::WorksheetAccessInterface
......@@ -223,6 +227,22 @@ CantorPart::CantorPart( QWidget *parentWidget, QObject *parent, const QVariantLi
connect(m_evaluate, &QAction::triggered, this, &CantorPart::evaluateOrInterrupt);
m_editActions.push_back(m_evaluate);
//
m_zoom = new KSelectAction(QIcon::fromTheme(QLatin1String("page-zoom")), i18n("Zoom"), collection);
connect(m_zoom, static_cast<void (KSelectAction::*)(const QString&)>(&KSelectAction::triggered), this, &CantorPart::zoomValueEdited);
static constexpr std::array<double, 8> ZoomValues = {0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 4.0};
QStringList zoomNames;
for (double zoomValue : ZoomValues)
{
const std::string& zoomName = std::to_string(static_cast<int>(zoomValue * 100)) + "%";
zoomNames << i18n(zoomName.c_str());
}
m_zoom->setItems(zoomNames);
m_zoom->setEditable(true);
Q_ASSERT(std::find(ZoomValues.begin(), ZoomValues.end(), 1.0) != ZoomValues.end());
m_zoom->setCurrentItem(std::distance(ZoomValues.begin(), std::find(ZoomValues.begin(), ZoomValues.end(), 1.0)));
collection->addAction(QLatin1String("zoom_selection_action"), m_zoom);
m_typeset = new KToggleAction(i18n("Typeset using LaTeX"), collection);
m_typeset->setChecked(Settings::self()->typesetDefault());
// Disable until login, because we use session command for this action
......@@ -1011,5 +1031,18 @@ void CantorPart::showImportantStatusMessage(const QString& message)
QTimer::singleShot(3000, this, SLOT(unblockStatusBar()));
}
void CantorPart::zoomValueEdited(const QString& text)
{
static const QRegularExpression zoomRegexp(QLatin1String("(?:(\\d+)%|(\\d+))"));
QRegularExpressionMatch match = zoomRegexp.match(text);
if (match.hasMatch())
{
double zoom = match.captured(1).toDouble() / 100.0;
if (m_worksheetview)
m_worksheetview->setScaleFactor(zoom);
}
}
K_PLUGIN_FACTORY_WITH_JSON(CantorPartFactory, "cantor_part.json", registerPlugin<CantorPart>();)
#include "cantor_part.moc"
......@@ -36,6 +36,7 @@ class ScriptEditorWidget;
class KAboutData;
class QAction;
class KToggleAction;
class KSelectAction;
class QProgressDialog;
namespace Cantor{
......@@ -116,6 +117,7 @@ protected Q_SLOTS:
void exportToLatex();
void evaluateOrInterrupt();
void restartBackend();
void zoomValueEdited(const QString& text);
void enableTypesetting(bool enable);
void showBackendHelp();
void print();
......@@ -169,6 +171,7 @@ private:
bool m_showProgressDlg;
QAction * m_evaluate;
QAction * m_restart;
KSelectAction* m_zoom;
QAction * m_save;
QAction * m_findNext;
QAction * m_findPrev;
......
......@@ -70,6 +70,7 @@
<Action name="evaluate_worksheet"/>
<Action name="restart_backend"/>
<Action name="backend_help"/>
<Action name="zoom_selection_action"/>
<Separator/>
</ToolBar>
<ToolBar noMerge="1" name="textEditToolBar">
......
......@@ -240,6 +240,13 @@ qreal WorksheetView::scaleFactor() const
return m_scale;
}
void WorksheetView::setScaleFactor(qreal zoom)
{
scale(1/m_scale * zoom, 1/m_scale * zoom);
m_scale = zoom;
updateSceneSize();
}
void WorksheetView::updateSceneSize()
{
QSize s = viewport()->size();
......
......@@ -44,6 +44,7 @@ public:
QPointF sceneCursorPos() const;
QRectF viewRect() const;
qreal scaleFactor() const;
void setScaleFactor(qreal);
void updateSceneSize();
Q_SIGNALS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment