Skip to content

Commit ee84b92

Browse files
author
MoonKyu Song
committed
Supporting mouse actions.
Following mouse actions are added. - Click on bottom/top is NextPage and PreviousPage. - Drag on side from edge to edge is Rotating Right or Left. - Drag from middle-center to bottom-center is Open. - Drag from middle-center to top-center is Close. - Drag from bottom-center to top-center is Quit. On file open dialog, parent directory button is attached. Make it ignore fast key input (ignoring accidental click).
1 parent 0118f35 commit ee84b92

12 files changed

+294
-53
lines changed

aconfig.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static CfgType configs[] = {
5959
{ GCT_INT, ACFG_ScrollPolicy, "ScrollPolicy", "3" },
6060
{ GCT_INT, ACFG_PagingPolicy, "PagingPolicy", "3" },
6161
{ GCT_BOOL, ACFG_ScaleUp, "ScaleUp", "0" },
62+
{ GCT_BOOL, ACFG_HoldDelay, "HoldDelay", "500" },
6263
{ GCT_END, -1, NULL, NULL }
6364
};
6465

aconfig.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum {
4646
ACFG_ScrollPolicy,
4747
ACFG_PagingPolicy,
4848
ACFG_ScaleUp,
49+
ACFG_HoldDelay,
4950
ACFG_Reserved
5051
};
5152

aview.cpp

+186-50
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ AView::AView(QMainWindow *parent,const char*name, WFlags f)
157157
}
158158
updateRescentFiles();
159159

160+
m_keyUsedTime.setToNow();
161+
160162
setFocusPolicy(QWidget::StrongFocus);
161163
this->setFocus();
162164
}
@@ -184,37 +186,64 @@ void
184186
AView::keyPressEvent(QKeyEvent *ev)
185187
{
186188
if (ev->isAutoRepeat()) return;
187-
printf("AView::keyPressEvent(0x%04x)\n", ev->key());
188-
switch (ev->key()) {
189+
//printf("AView::keyPressEvent(0x%04x)\n", ev->key());
190+
m_keyPressed = ev->key();
191+
m_keyPressTime.setToNow();
192+
}
193+
194+
bool
195+
AView::doAction(int key_code, int holding)
196+
{
197+
bool deepPress = holding>700;
198+
199+
// Too fast key press need to be ignored.
200+
if (m_keyUsedTime.passed()<200) {
201+
m_keyUsedTime.setToNow();
202+
return false;
203+
}
204+
m_keyUsedTime.setToNow();
205+
206+
switch(key_code) {
189207
case Qt::Key_Space:
190208
m_view->nextPage();
191209
break;
210+
case Qt::Key_Escape:
211+
if (deepPress)
212+
this->closeNow();
213+
else
214+
this->closeFile();
215+
break;
216+
case Qt::Key_Return:
217+
if (deepPress)
218+
this->showPopup();
219+
else
220+
this->toggleFullScreen();
221+
break;
192222
case Qt::Key_Down:
193223
if (!m_view->hasDocument()) {
194224
openFile();
195225
} else {
196-
m_keyPressed = ev->key();
197-
m_keyTime.setToNow();
226+
if (deepPress)
227+
m_view->setSlideShowMode(2);
228+
else
229+
m_view->nextPage();
198230
}
199231
break;
200232
case Qt::Key_Up:
201233
if (!m_view->hasDocument()) {
202234
openFile();
203235
} else {
204-
m_keyPressed = ev->key();
205-
m_keyTime.setToNow();
236+
if (deepPress) {
237+
showJump();
238+
} else {
239+
m_view->prevPage();
240+
}
206241
}
207242
break;
208243
case Qt::Key_Right:
209244
m_view->rotateRight();
210245
break;
211246
case Qt::Key_Left:
212-
m_view->rotateLeft();
213-
break;
214-
case Qt::Key_F33:
215-
case Qt::Key_Return:
216-
m_keyPressed = Qt::Key_Return;
217-
m_keyTime.setToNow();
218247
break;
219248
case Qt::Key_O:
220249
openFile();
@@ -224,10 +253,6 @@ AView::keyPressEvent(QKeyEvent *ev)
224253
break;
225254
case Qt::Key_S:
226255
m_view->setSlideShowMode(2);
227-
return;
228-
case Qt::Key_Escape:
229-
m_keyPressed = Qt::Key_Escape;
230-
m_keyTime.setToNow();
231256
break;
232257
case Qt::Key_G:
233258
showJump();
@@ -236,61 +261,35 @@ AView::keyPressEvent(QKeyEvent *ev)
236261
showSearch();
237262
break;
238263
default:
239-
ev->ignore();
240-
break;
264+
return false;
241265
}
242-
m_view->setSlideShowMode(0);
266+
return true;
243267
}
244268

245269
void
246270
AView::keyReleaseEvent(QKeyEvent *ev)
247271
{
248272
if (ev->isAutoRepeat()) return;
249-
printf("AView::keyReleaseEvent(0x%04x)\n", ev->key());
250273

251274
int holding = 0;
252275
int p_key = ev->key();
276+
277+
//printf("AView::keyReleaseEvent(0x%04x)\n", ev->key());
253278
if (p_key==Qt::Key_F33)
254279
p_key = Qt::Key_Return;
255280

256281
if (m_keyPressed==p_key) {
257-
holding = m_keyTime.passed();
282+
holding = m_keyPressTime.passed();
258283
} else {
259284
m_keyPressed = Qt::Key_unknown;
260285
ev->ignore();
261286
return;
262287
}
263288
m_keyPressed = Qt::Key_unknown;
264289

265-
switch(p_key) {
266-
case Qt::Key_Escape:
267-
if (holding>500)
268-
this->closeNow();
269-
else
270-
this->closeFile();
271-
break;
272-
case Qt::Key_Return:
273-
if (holding>500)
274-
this->showPopup();
275-
else
276-
this->toggleFullScreen();
277-
break;
278-
case Qt::Key_Down:
279-
if (holding>500)
280-
m_view->setSlideShowMode(2);
281-
else
282-
m_view->nextPage();
283-
break;
284-
case Qt::Key_Up:
285-
if (holding>500) {
286-
showJump();
287-
} else {
288-
m_view->prevPage();
289-
}
290-
break;
291-
default:
292-
ev->ignore();
293-
break;
290+
printf("AView::keyEvent(code=0x%04x,holding=%d)\n", p_key, holding);
291+
if (!doAction(p_key, holding)) {
292+
ev->ignore();
294293
}
295294
}
296295

@@ -714,3 +713,140 @@ AView::loadPrev()
714713
openFile(new_file, 0, 0);
715714
}
716715
}
716+
717+
void
718+
AView::viewMousePressEvent(QMouseEvent *ev)
719+
{
720+
m_mousePressed = ev->button();
721+
m_mouseMove = false;
722+
m_mousePressTime.setToNow();
723+
m_mx = ev->x();
724+
m_my = ev->y();
725+
}
726+
727+
728+
int
729+
AView::getAreaIndex(int x, int y, int width, int height, int rotation)
730+
{
731+
int nx, ny, nwidth, nheight;
732+
if (rotation<0) rotation = m_view->getRotation();
733+
while (rotation<0) rotation += 360;
734+
while (rotation>=360) rotation -= 360;
735+
switch (rotation) {
736+
case 0:
737+
nx = x; ny = y; nwidth = width; nheight = height;
738+
break;
739+
case 90:
740+
ny = width-x; nx = y; nwidth = height; nheight = width;
741+
break;
742+
case 180:
743+
nx = width-x; ny = height-y; nwidth = width; nheight = height;
744+
break;
745+
case 270:
746+
ny = x; nx = height-y; nwidth = height; nheight = width;
747+
break;
748+
}
749+
printf("AView::getAreaIndex(nx=%d,ny=%d,nw=%d,nh=%d,rot=%d)\n",
750+
nx, ny, nwidth, nheight,rotation);
751+
752+
int idx = 0;
753+
if (ny>(nheight*1/3)) idx += 3;
754+
if (ny>(nheight*2/3)) idx += 3;
755+
if (nx>(nwidth*1/3)) idx += 1;
756+
if (nx>(nwidth*2/3)) idx += 1;
757+
758+
return idx;
759+
}
760+
761+
#define MOVEKEY(from,to) ((from)*9+(to))
762+
#define TOP_LEFT 0
763+
#define TOP_CENTER 1
764+
#define TOP_RIGHT 2
765+
#define MIDDLE_LEFT 3
766+
#define MIDDLE_CENTER 4
767+
#define MIDDLE_RIGHT 5
768+
#define BOTTOM_LEFT 6
769+
#define BOTTOM_CENTER 7
770+
#define BOTTOM_RIGHT 8
771+
void
772+
AView::viewMouseReleaseEvent(QMouseEvent *ev)
773+
{
774+
if (m_mouseMove) {
775+
m_mousePressed = 0;
776+
int fidx = getAreaIndex(m_mx,m_my,m_view->width(),m_view->height());
777+
int tidx = getAreaIndex(ev->x(),ev->y(),m_view->width(), m_view->height());
778+
switch (MOVEKEY(fidx,tidx)) {
779+
case MOVEKEY(BOTTOM_LEFT,BOTTOM_RIGHT):
780+
case MOVEKEY(BOTTOM_RIGHT,TOP_RIGHT):
781+
case MOVEKEY(TOP_RIGHT,TOP_LEFT):
782+
case MOVEKEY(TOP_LEFT,BOTTOM_LEFT):
783+
rotateLeft();
784+
m_keyUsedTime.setToNow();
785+
break;
786+
case MOVEKEY(BOTTOM_RIGHT,BOTTOM_LEFT):
787+
case MOVEKEY(TOP_RIGHT,BOTTOM_RIGHT):
788+
case MOVEKEY(TOP_LEFT,TOP_RIGHT):
789+
case MOVEKEY(BOTTOM_LEFT,TOP_LEFT):
790+
rotateRight();
791+
m_keyUsedTime.setToNow();
792+
break;
793+
case MOVEKEY(MIDDLE_CENTER,MIDDLE_LEFT):
794+
case MOVEKEY(MIDDLE_RIGHT,MIDDLE_CENTER):
795+
case MOVEKEY(MIDDLE_RIGHT,MIDDLE_LEFT):
796+
loadNext();
797+
m_keyUsedTime.setToNow();
798+
break;
799+
case MOVEKEY(MIDDLE_CENTER,MIDDLE_RIGHT):
800+
case MOVEKEY(MIDDLE_LEFT,MIDDLE_CENTER):
801+
case MOVEKEY(MIDDLE_LEFT,MIDDLE_RIGHT):
802+
loadPrev();
803+
m_keyUsedTime.setToNow();
804+
break;
805+
case MOVEKEY(MIDDLE_CENTER,TOP_CENTER):
806+
closeFile();
807+
m_keyUsedTime.setToNow();
808+
break;
809+
case MOVEKEY(MIDDLE_CENTER,BOTTOM_CENTER):
810+
openFile();
811+
m_keyUsedTime.setToNow();
812+
break;
813+
case MOVEKEY(BOTTOM_CENTER,TOP_CENTER):
814+
closeNow();
815+
break;
816+
}
817+
return;
818+
}
819+
820+
int holding = m_mousePressTime.passed();
821+
printf("AView::click!!! (%d,%d) holding=%d\n", ev->x(), ev->y(), holding);
822+
823+
switch (getAreaIndex(ev->x(), ev->y(), m_view->width(), m_view->height())) {
824+
case MIDDLE_CENTER: // center
825+
doAction(QKeyEvent::Key_Return, holding);
826+
break;
827+
case TOP_LEFT:
828+
case TOP_CENTER:
829+
case TOP_RIGHT:
830+
doAction(QKeyEvent::Key_Up, holding);
831+
break;
832+
case BOTTOM_LEFT:
833+
case BOTTOM_CENTER:
834+
case BOTTOM_RIGHT:
835+
doAction(QKeyEvent::Key_Down, holding);
836+
break;
837+
}
838+
}
839+
840+
void
841+
AView::viewMouseMoveEvent(QMouseEvent *ev)
842+
{
843+
int dx, dy, dist;
844+
if (m_mousePressed==0) return;
845+
dx = m_mx-ev->x();
846+
dy = m_my-ev->y();
847+
dist = (int)(sqrt(dx*dx+dy*dy)+0.5f);
848+
if (dist > 16) {
849+
//printf("AView::moved to far %d\n", dist);
850+
m_mouseMove = true;
851+
}
852+
}

aview.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ class AView : public QMainWindow
6565

6666
/* key press status */
6767
int m_keyPressed;
68-
TimeStamp m_keyTime;
68+
TimeStamp m_keyPressTime, m_keyUsedTime;
69+
70+
int m_mousePressed;
71+
bool m_mouseMove;
72+
TimeStamp m_mousePressTime;
73+
int m_mx, m_my;
6974

7075
/* ui status */
7176
bool m_fullScreen, m_fullBeforeDialog;
7277
int m_tbHeight;
7378

7479
private:
80+
bool doAction(int key, int holding);
81+
7582
void resizeEvent(QResizeEvent *ev);
7683
void keyPressEvent(QKeyEvent *ev);
7784
void keyReleaseEvent(QKeyEvent *ev);
@@ -80,7 +87,8 @@ class AView : public QMainWindow
8087
void showScreen(bool isFull);
8188
void updateViewSize();
8289
void showView();
83-
90+
int getAreaIndex(int x, int y, int width, int height,
91+
int rotation=-1);
8492
public slots:
8593
void openFile();
8694
void closeFile();
@@ -110,6 +118,10 @@ class AView : public QMainWindow
110118
AView(QMainWindow *parent=NULL, const char *name=NULL,
111119
WFlags f=WType_TopLevel);
112120
~AView();
121+
122+
void viewMousePressEvent(QMouseEvent *ev);
123+
void viewMouseReleaseEvent(QMouseEvent *ev);
124+
void viewMouseMoveEvent(QMouseEvent *ev);
113125
};
114126

115127
#endif

baseview.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BaseView : public QWidget
3737
virtual void prevPage() = 0;
3838
virtual void rotateRight() = 0;
3939
virtual void rotateLeft() = 0;
40+
virtual int getRotation() { return 0; };
4041
signals:
4142
void loadNext();
4243
void loadPrev();

0 commit comments

Comments
 (0)