]> Shamusworld >> Repos - ttedit/blob - src/toolwindow.cpp
Added rotation tool, save/load capability.
[ttedit] / src / toolwindow.cpp
1 //
2 // TOOLWINDOW.CPP - Tool Palette window
3 // by James L. Hammons
4 // (C) 2008 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  08/28/2008  Created this file
11 // JLH  03/11/2009  Converted from wxWidgets to Qt
12 //
13
14 // FIXED:
15 //
16 // STILL TO BE DONE:
17 //
18 //
19
20 // Uncomment this for debugging...
21 #define DEBUG
22 #define DEBUGFOO            // Various tool debugging...
23 #define DEBUGTP                         // Toolpalette debugging...
24
25 #include "toolwindow.h"
26 #include "types.h"
27
28
29 ToolWindow::ToolWindow(void): QWidget(NULL, Qt::Window | Qt::FramelessWindowHint),
30         prevTool(TOOLNone)
31 {
32         img = QImage(":/res/toolpal1.xpm");
33
34         // Set up sizes
35
36         sizeTPBM.rx() = img.width(), sizeTPBM.ry() = img.height();
37         sizeStamp.rx() = sizeTPBM.x() / 5, sizeStamp.ry() = sizeTPBM.y() / 2;
38
39         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
40         setVisible(false);
41 }
42
43 QSize ToolWindow::sizeHint() const
44 {
45         return QSize(sizeTPBM.x(), sizeTPBM.y());
46 }
47
48 void ToolWindow::paintEvent(QPaintEvent * event)
49 {
50         QPainter p(this);
51         p.drawImage(QPoint(0, 0), img);
52
53         if (prevTool != -1)
54         {
55             //need ul corner of bitmap, ul corner of dest, width/height
56                 p.setCompositionMode(QPainter::RasterOp_NotSource);
57 //              QPoint pt(sizeStamp.x() * (prevTool & 0x03), sizeStamp.y() * (prevTool >> 2));
58                 QPoint pt(sizeStamp.x() * (prevTool % 5), sizeStamp.y() * (prevTool / 5));
59                 p.drawImage(pt.x(), pt.y(), img, pt.x(), pt.y(), sizeStamp.x(), sizeStamp.y());
60         }
61 }
62
63 //
64 // Find which tool we're pointing at
65 //
66 ToolType ToolWindow::FindSelectedTool(void)
67 {
68         // Get mouse coords relative to the tool palette window
69 //      wxPoint pt = ScreenToClient(wxGetMousePosition());
70         QPoint pt = mapFromGlobal(QCursor::pos());
71 //printf("pt = %u, %u\n", pt.x(), pt.y());
72
73         // Divide mouse coords by the bitmap stamp size to find which one is pointed to
74         uint32 x = (uint32)pt.x() / sizeStamp.x(), y = (uint32)pt.y() / sizeStamp.y();
75
76         // Preset failure into newTool, in case no new tool is selected
77         ToolType newTool = TOOLNone;
78
79         // NOTE: This works because x and y are UNSIGNED
80         if (x < 5 && y < 2)
81                 newTool = (ToolType)((y * 5) + x);
82
83         // We don't have 10 yet, so fix this if the user selected the blank space
84         if (newTool == 9)
85                 newTool = TOOLNone;
86
87         return newTool;
88 }