interactive number adjust

When the cursor is on number, you can increment or decrement it with Alt-Up or Alt-Down keys
and see the change of model in real time.
The Alt-Left and Alt-Right keys navigates to left or right within the number and allow you to change
the increment value. Navigating to right over the number, new decimal digits are added automatically.
When the update of preview is slow and you change the number quickly, the frame rate is limited, however
when you stop to increment, the actual model is shown as soon as possible.
This commit is contained in:
Josef Pavlik 2015-10-30 09:14:17 +01:00
parent 53c255d4fd
commit 3b64b3c370
3 changed files with 141 additions and 1 deletions

View file

@ -218,6 +218,7 @@ MainWindow::MainWindow(const QString &filename)
Preferences::inst()->fireEditorConfigChanged();
}
#endif
connect(editor, SIGNAL(previewRequest()), this, SLOT(actionRenderPreview()));
editorDockContents->layout()->addWidget(editor);
@ -1593,7 +1594,6 @@ void MainWindow::findBufferChanged() {
}
}
bool MainWindow::eventFilter(QObject* obj, QEvent *event)
{
if (obj == find_panel)
@ -1803,9 +1803,13 @@ void MainWindow::csgReloadRender()
void MainWindow::actionRenderPreview()
{
static bool preview_requested;
preview_requested=true;
if (GuiLocker::isLocked()) return;
GuiLocker::lock();
autoReloadTimer->stop();
preview_requested=false;
setCurrentOutput();
PRINT("Parsing design (AST generation)...");
@ -1813,6 +1817,12 @@ void MainWindow::actionRenderPreview()
this->afterCompileSlot = "csgRender";
this->procevents = !viewActionAnimate->isChecked();
compile(false);
if (preview_requested) {
// if the action was called when the gui was locked, we must request it one more time
// however, it's not possible to call it directly NOR make the loop
// it must be called from the mainloop
QTimer::singleShot(0, this, SLOT(actionRenderPreview()));
}
}
void MainWindow::csgRender()

View file

@ -149,6 +149,7 @@ ScintillaEditor::ScintillaEditor(QWidget *parent) : EditorInterface(parent)
connect(qsci, SIGNAL(textChanged()), this, SIGNAL(contentsChanged()));
connect(qsci, SIGNAL(modificationChanged(bool)), this, SIGNAL(modificationChanged(bool)));
qsci->installEventFilter(this);
}
/**
@ -602,3 +603,125 @@ QString ScintillaEditor::selectedText()
{
return qsci->selectedText();
}
bool ScintillaEditor::eventFilter(QObject* obj, QEvent *e)
{
if (obj != qsci) return EditorInterface::eventFilter(obj, e);
if ( e->type()==QEvent::KeyPress
|| e->type()==QEvent::KeyRelease
) {
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
if ( ke->modifiers()==Qt::AltModifier )
{
switch (ke->key())
{
case Qt::Key_Left:
case Qt::Key_Right:
if (e->type()==QEvent::KeyPress) {
navigateOnNumber(ke->key());
}
return true;
case Qt::Key_Up:
case Qt::Key_Down:
if (e->type()==QEvent::KeyPress) {
modifyNumber(ke->key());
}
return true;
}
}
}
return false;
}
void ScintillaEditor::navigateOnNumber(int key)
{
int line, index;
qsci->getCursorPosition(&line, &index);
QString text=qsci->text(line);
QString left=text.left(index);
bool dotOnLeft=left.indexOf(QRegExp("\\.\\d*$"))>=0;
bool dotJustLeft=index>1 && text[index-2]=='.';
bool dotJustRight=text[index]=='.';
bool numOnLeft=left.indexOf(QRegExp("\\d\\.?$"))>=0 || left.endsWith("-.");
bool numOnRight=text.indexOf(QRegExp("\\.?\\d"),index)==index;
switch (key)
{
case Qt::Key_Left:
if (numOnLeft)
qsci->setCursorPosition(line, index-(dotJustLeft?2:1));
break;
case Qt::Key_Right:
if (numOnRight)
qsci->setCursorPosition(line, index+(dotJustRight?2:1));
else if (numOnLeft) {
// add trailing zero
if (!dotOnLeft) {
qsci->insert(".0");
index++;
} else {
qsci->insert("0");
}
qsci->setCursorPosition(line, index+1);
}
break;
}
}
void ScintillaEditor::modifyNumber(int key)
{
int line, index;
qsci->getCursorPosition(&line, &index);
QString text=qsci->text(line);
int lineFrom, indexFrom, lineTo, indexTo;
qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
bool hadSelection=qsci->hasSelectedText();
int begin=QRegExp("[-+]?\\d*\\.?\\d*$").indexIn(text.left(index));
int end=text.indexOf(QRegExp("[^0-9.]"),index);
if (end<0) end=text.length();
QString nr=text.mid(begin,end-begin);
bool sign=nr[0]=='+'||nr[0]=='-';
if (nr.endsWith('.')) nr=nr.left(nr.length()-1);
int curpos=index-begin;
int dotpos=nr.indexOf('.');
int decimals=dotpos<0?0:nr.length()-dotpos-1;
int number=(dotpos<0)?nr.toInt():(nr.left(dotpos)+nr.mid(dotpos+1)).toInt();
int tail=nr.length()-curpos;
int exponent=tail-((dotpos>=curpos)?1:0);
int step=1;
for (int i=exponent; i>0; i--) step*=10;
switch (key) {
case Qt::Key_Up: number+=step; break;
case Qt::Key_Down: number-=step; break;
}
bool negative=number<0;
if (negative) number=-number;
QString newnr=QString::number(number);
if (decimals) {
if (newnr.length()<=decimals) newnr.prepend(QString(decimals-newnr.length()+1,'0'));
newnr=newnr.left(newnr.length()-decimals)+"."+newnr.right(decimals);
}
if (tail>newnr.length()) {
newnr.prepend(QString(tail-newnr.length(),'0'));
}
if (negative) newnr.prepend('-');
else if (sign) newnr.prepend('+');
qsci->setSelection(line, begin, line, end);
qsci->replaceSelectedText(newnr);
qsci->selectAll(false);
if (hadSelection)
{
qsci->setSelection(lineFrom, indexFrom, lineTo, indexTo);
}
qsci->setCursorPosition(line, begin+newnr.length()-tail);
emit previewRequest();
}

View file

@ -63,6 +63,13 @@ private:
QColor readColor(const boost::property_tree::ptree &pt, const std::string name, const QColor defaultColor);
void enumerateColorSchemesInPath(colorscheme_set_t &result_set, const fs::path path);
colorscheme_set_t enumerateColorSchemes();
virtual bool eventFilter(QObject* obj, QEvent *event);
void navigateOnNumber(int key);
void modifyNumber(int key);
signals:
void previewRequest(void);
public slots:
void zoomIn();