Theme engine using TQt for GTK+ 3.x
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-painter.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include <cairo.h>
  2. #include <tqpainter.h>
  3. #include <tqapplication.h>
  4. #include <tqstyle.h>
  5. #include "tqtcairopainter.h"
  6. // TQt3 test image only
  7. #include <tqpixmap.h>
  8. void drawColorWheel(TQPainter *p, double scale)
  9. {
  10. TQFont f( "times", 18, TQFont::Bold );
  11. p->setFont( f );
  12. p->setPen(TQt::black);
  13. p->setWindow( 0, 0, 500*scale, 500*scale ); // defines coordinate system
  14. for ( int i=0; i<36; i++ ) { // draws 36 rotated rectangles
  15. TQWMatrix matrix;
  16. matrix.translate( 250.0F*scale, 250.0F*scale ); // move to center
  17. matrix.shear( 0.0F*scale, 0.3F*scale ); // twist it
  18. matrix.rotate( (float)i*10 ); // rotate 0,10,20,.. degrees
  19. p->setWorldMatrix( matrix ); // use this world matrix
  20. TQColor c;
  21. c.setHsv( i*10, 255, 255 ); // rainbow effect
  22. p->setBrush( c ); // solid fill with color c
  23. p->drawRect( 70*scale, -10*scale, 80*scale, 10*scale ); // draw the rectangle
  24. TQString n;
  25. n.sprintf( "H=%d", i*10 );
  26. p->drawText( (80+70+5)*scale, 0, n ); // draw the hue number
  27. }
  28. }
  29. void runTests(TQPaintDevice* pd) {
  30. TQPainter p(pd);
  31. // Background
  32. {
  33. p.setPen( TQt::NoPen );
  34. TQBrush brush(TQColor(0,0,0), TQBrush::SolidPattern);
  35. p.setBrush(brush);
  36. p.drawRect(0,0,1000,1000);
  37. }
  38. // Rectangle tests
  39. {
  40. p.setPen(TQPen(TQt::red, 1));
  41. p.drawRect(5, 5, 50, 50);
  42. TQBrush brush( TQt::yellow, TQBrush::DiagCrossPattern ); // yellow pattern
  43. p.setBrush( brush ); // set the yellow brush
  44. p.setPen( TQt::NoPen ); // do not draw outline
  45. p.drawRect( 0,0, 25,25 ); // draw filled rectangle
  46. p.drawRect( 452,400, 25,25 ); // draw filled rectangle
  47. TQBrush brush2( TQt::green, TQBrush::SolidPattern ); // green pattern
  48. p.setBrush( brush2 ); // set the green brush
  49. p.setPen( TQt::NoPen ); // do not draw outline
  50. p.drawRect( 40,30, 200,100 ); // draw filled rectangle
  51. p.setBrush( TQt::NoBrush ); // do not fill
  52. p.setPen( TQt::blue ); // set blue pen, 0 pixel width
  53. p.drawRect( 10,10, 30,20 ); // draw rectangle outline
  54. TQBrush brush3( TQColor(255,128,0), TQBrush::SolidPattern ); // green pattern
  55. p.setBrush( brush3 ); // set the yellow brush
  56. p.setPen( TQColor(255,0,255) ); // draw outline
  57. p.drawRect( 50,40, 180,80 ); // draw filled rectangle
  58. }
  59. // Line tests
  60. {
  61. p.setPen( TQt::blue ); // set blue pen, 0 pixel width
  62. p.drawLine(140,140, 160,160);
  63. p.setPen( TQt::blue ); // set blue pen, 0 pixel width
  64. p.drawLine(470,140, 480,160);
  65. p.setPen( TQt::blue ); // set blue pen, 0 pixel width
  66. p.drawLine(470,170, 490,170);
  67. p.setPen( TQPen(TQt::blue, 1) ); // set blue pen, 1 pixel width
  68. p.drawLine(470,180, 490,180);
  69. p.setPen( TQPen(TQt::blue, 2) ); // set blue pen, 2 pixel width
  70. p.drawLine(470,190, 490,190);
  71. p.setPen( TQPen(TQt::blue, 3) ); // set blue pen, 3 pixel width
  72. p.drawLine(470,200, 490,200);
  73. p.setPen( TQPen(TQt::blue, 4) ); // set blue pen, 4 pixel width
  74. p.drawLine(470,210, 490,210);
  75. p.setPen( TQPen(TQt::blue, 5) ); // set blue pen, 5 pixel width
  76. p.drawLine(470,220, 490,220);
  77. p.setPen( TQPen(TQt::blue, 6) ); // set blue pen, 6 pixel width
  78. p.drawLine(470,230, 490,230);
  79. }
  80. // Point tests
  81. {
  82. p.setPen( TQt::green ); // set green pen, 0 pixel width
  83. p.drawPoint(135,135);
  84. }
  85. // Polyline tests
  86. {
  87. TQPointArray a;
  88. int x1 = 250;
  89. int y1 = 10;
  90. a.setPoints( 11, x1+0, y1+85, x1+75, y1+75, x1+100, y1+10, x1+125, y1+75, x1+200, y1+85, x1+150, y1+125, x1+160, y1+190, x1+100, y1+150, x1+40, y1+190, x1+50, y1+125, x1+0, y1+85 );
  91. p.setPen(TQt::red);
  92. p.drawPolyline(a);
  93. }
  94. // Polyfill tests
  95. {
  96. TQPointArray a;
  97. int x1 = 250;
  98. int y1 = 200;
  99. a.setPoints( 11, x1+0, y1+85, x1+75, y1+75, x1+100, y1+10, x1+125, y1+75, x1+200, y1+85, x1+150, y1+125, x1+160, y1+190, x1+100, y1+150, x1+40, y1+190, x1+50, y1+125, x1+0, y1+85 );
  100. p.setPen(TQt::blue);
  101. p.setBrush(TQt::green);
  102. p.drawPolygon(a);
  103. }
  104. // Rounded rectangle tests
  105. {
  106. p.setBrush(TQt::green);
  107. p.setPen(TQPen(TQt::red, 1));
  108. p.drawRoundRect(10, 150, 50, 50);
  109. p.setBrush(TQBrush());
  110. p.setPen(TQPen(TQt::blue, 1));
  111. p.drawRoundRect(80, 150, 50, 50);
  112. p.setBrush(TQt::green);
  113. p.setPen(TQPen());
  114. p.drawRoundRect(150, 150, 50, 50);
  115. }
  116. // Ellipse tests
  117. {
  118. p.setBrush(TQt::green);
  119. p.setPen(TQPen(TQt::red, 1));
  120. p.drawEllipse(10, 220, 50, 50);
  121. p.setBrush(TQBrush());
  122. p.setPen(TQPen(TQt::blue, 1));
  123. p.drawEllipse(80, 220, 50, 50);
  124. p.setBrush(TQt::green);
  125. p.setPen(TQPen());
  126. p.drawEllipse(150, 220, 50, 50);
  127. }
  128. // Arc tests
  129. {
  130. p.setBrush(TQBrush());
  131. p.setPen(TQPen(TQt::yellow, 1));
  132. p.drawArc(10,10, 70,100, 100*16, 160*16); // draws a "(" arc
  133. }
  134. // Pie tests
  135. {
  136. p.setBrush(TQBrush());
  137. p.setPen(TQPen(TQt::red, 1));
  138. p.drawPie(250, 400, 200, 100, 45*16, 90*16);
  139. p.setBrush(TQt::green);
  140. p.setPen(TQPen(TQt::blue, 1));
  141. p.drawPie(250, 450, 200, 100, 45*16, 90*16);
  142. }
  143. // Chord tests
  144. {
  145. p.setBrush(TQBrush());
  146. p.setPen(TQPen(TQt::red, 1));
  147. p.drawChord(100, 400, 200, 100, 45*16, 90*16);
  148. p.setBrush(TQt::green);
  149. p.setPen(TQPen(TQt::blue, 1));
  150. p.drawChord(100, 450, 200, 100, 45*16, 90*16);
  151. }
  152. // Line segment tests
  153. {
  154. TQPointArray a;
  155. int x1 = 10;
  156. int y1 = 400;
  157. a.setPoints( 4, x1+0, y1+85, x1+75, y1+75, x1+0, y1+95, x1+75, y1+85 );
  158. p.setPen(TQColor(255,128,0));
  159. p.drawLineSegments(a);
  160. }
  161. // Bezier curve tests
  162. {
  163. TQPointArray a;
  164. int x1 = 100;
  165. int y1 = 400;
  166. a.setPoints( 4, x1+0, y1+85, x1+75, y1+75, x1+0, y1+95, x1+75, y1+85 );
  167. p.setPen(TQColor(0,128,255));
  168. p.drawCubicBezier(a);
  169. }
  170. #if 1
  171. // Pixmap tests
  172. {
  173. TQPixmap pixmap("open.png");
  174. p.drawPixmap(200, 10, pixmap, 0, 0, -1, -1);
  175. }
  176. // Image tests
  177. {
  178. TQImage image("open.png");
  179. p.drawImage(350, 10, image, 0, 0, -1, -1);
  180. }
  181. #endif
  182. #if 0
  183. // Font tests
  184. {
  185. p.setPen(TQColor(0,128,255));
  186. static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 };
  187. static int sizes[] = { 10, 12, 18, 24, 36, 0 };
  188. int f = 0;
  189. int y = 0;
  190. while ( fonts[f] ) {
  191. int s = 0;
  192. while ( sizes[s] ) {
  193. TQFont font( fonts[f], sizes[s] );
  194. p.setFont( font );
  195. TQFontMetrics fm = p.fontMetrics();
  196. y += fm.ascent();
  197. p.drawText( 10, y, "TQt3 renders via Cairo!" );
  198. y += fm.descent();
  199. s++;
  200. if (y > 500) {
  201. break;
  202. }
  203. }
  204. f++;
  205. if (y > 500) {
  206. break;
  207. }
  208. }
  209. //p.setPen(TQColor(255,128,0));
  210. //p.drawText( TQRect(250, 250, 250, 150), TQt::BreakAnywhere, TQString("TQt3 renders via Cairo!") );
  211. //p.setPen(TQColor(255,0,128));
  212. //p.drawText( TQRect(0, 250, 250, 150), TQt::BreakAnywhere | TQt::AlignCenter, TQString("TQt3 renders via Cairo!") );
  213. //p.drawText( TQRect(0, 250, 250, 150), TQt::BreakAnywhere | TQt::AlignHCenter | TQt::AlignBottom, TQString("TQt3 renders via Cairo!") );
  214. }
  215. #endif
  216. // Clipping tests
  217. {
  218. p.setBrush(TQBrush(TQt::green));
  219. p.setPen(TQPen(TQt::blue, 1));
  220. TQRect boundary(400, 10, 100, 100);
  221. TQRegion rectRegion(425,35,50,50);
  222. p.setClipRegion(rectRegion);
  223. p.fillRect(boundary, TQBrush(TQt::green));
  224. p.setClipping(false);
  225. }
  226. // Raster operation tests
  227. {
  228. p.setRasterOp(TQPainter::XorROP);
  229. p.setBrush(TQBrush(TQt::white));
  230. p.setPen(TQPen());
  231. p.drawRect(325, 275, 50, 50);
  232. p.setRasterOp(TQPainter::CopyROP);
  233. }
  234. //drawColorWheel(&p, 0.5);
  235. //drawColorWheel(&p, 1.0);
  236. TQColorGroup cg;
  237. cg.setColor(TQColorGroup::Background, TQColor(128,128,128));
  238. cg.setColor(TQColorGroup::Foreground, TQColor(0,0,0));
  239. tqApp->style().drawPrimitive(TQStyle::PE_ExclusiveIndicator, &p, TQRect(400, 400, 16, 16), cg, TQStyle::Style_Down);
  240. // Tab bar tab tests
  241. {
  242. int currentPage;
  243. int numPages=3;
  244. int tabIndex=2;
  245. int x = 0;
  246. int y = 300;
  247. int width = 50;
  248. int height = 20;
  249. for (currentPage=0;currentPage<numPages;currentPage++) {
  250. TQRect boundingRect(0, 0, width, height);
  251. TQStringList objectTypes;
  252. objectTypes.append(TQTABBAR_OBJECT_NAME_STRING);
  253. TQPalette objectPalette = tqApp->palette(objectTypes);
  254. TQStyleControlElementData ceData;
  255. TQStyle::ControlElementFlags elementFlags;
  256. ceData.widgetObjectTypes = objectTypes;
  257. ceData.rect = boundingRect;
  258. TQTab tqt3Tab;
  259. tqt3Tab.setIdentifier(currentPage);
  260. TQStyleOption tabOpt(&tqt3Tab, (TQTab*)NULL);
  261. elementFlags = elementFlags | TQStyle::CEF_HasParentWidget;
  262. ceData.parentWidgetData.widgetObjectTypes.append(TQTABBAR_OBJECT_NAME_STRING);
  263. int tab_overlap = tqApp->style().pixelMetric(TQStyle::PM_TabBarTabOverlap);
  264. x = x + width + tab_overlap;
  265. boundingRect = TQRect(x, y, width+(tab_overlap*2), height);
  266. ceData.tabBarData.shape = TQTabBar::RoundedAbove;
  267. ceData.tabBarData.tabCount = numPages;
  268. ceData.tabBarData.identIndexMap[tqt3Tab.identifier()] = currentPage;
  269. TQColorGroup cg = tqApp->palette().active();
  270. // Draw tab
  271. tqApp->style().drawControl(TQStyle::CE_TabBarTab, &p, ceData, elementFlags, boundingRect, cg, ((tabIndex==currentPage)?TQStyle::Style_Selected:TQStyle::Style_Default), tabOpt);
  272. }
  273. }
  274. {
  275. int currentPage;
  276. int numPages=3;
  277. int tabIndex=2;
  278. int x = (50*4);
  279. int y = 325;
  280. int width = 50;
  281. int height = 20;
  282. for (currentPage=(numPages-1);currentPage>=0;currentPage--) {
  283. TQRect boundingRect(0, 0, width, height);
  284. TQStringList objectTypes;
  285. objectTypes.append(TQTABBAR_OBJECT_NAME_STRING);
  286. TQPalette objectPalette = tqApp->palette(objectTypes);
  287. TQStyleControlElementData ceData;
  288. TQStyle::ControlElementFlags elementFlags;
  289. ceData.widgetObjectTypes = objectTypes;
  290. ceData.rect = boundingRect;
  291. TQTab tqt3Tab;
  292. tqt3Tab.setIdentifier(currentPage);
  293. TQStyleOption tabOpt(&tqt3Tab, (TQTab*)NULL);
  294. elementFlags = elementFlags | TQStyle::CEF_HasParentWidget;
  295. ceData.parentWidgetData.widgetObjectTypes.append(TQTABBAR_OBJECT_NAME_STRING);
  296. int tab_overlap = tqApp->style().pixelMetric(TQStyle::PM_TabBarTabOverlap);
  297. x = x - width - tab_overlap;
  298. boundingRect = TQRect(x, y, width+(tab_overlap*2), height);
  299. ceData.tabBarData.shape = TQTabBar::RoundedAbove;
  300. ceData.tabBarData.tabCount = numPages;
  301. ceData.tabBarData.identIndexMap[tqt3Tab.identifier()] = currentPage;
  302. TQColorGroup cg = tqApp->palette().active();
  303. // Draw tab
  304. tqApp->style().drawControl(TQStyle::CE_TabBarTab, &p, ceData, elementFlags, boundingRect, cg, ((tabIndex==currentPage)?TQStyle::Style_Selected:TQStyle::Style_Default), tabOpt);
  305. }
  306. }
  307. p.end();
  308. }
  309. int
  310. main (int argc, char *argv[])
  311. {
  312. cairo_surface_t *surface;
  313. cairo_t *cr;
  314. surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 500, 500);
  315. // cr = cairo_create (surface);
  316. // /* Examples are in 1.0 x 1.0 coordinate space */
  317. // cairo_scale (cr, 120, 120);
  318. //
  319. // /* Drawing code goes here */
  320. // cairo_set_line_width (cr, 0.1);
  321. // cairo_set_source_rgb (cr, 0, 0, 0);
  322. // cairo_rectangle (cr, 0.25, 0.25, 0.5, 0.5);
  323. // cairo_stroke (cr);
  324. // Initialize TQApplication required data structures
  325. new TQApplication(argc, argv, TRUE);
  326. TQt3CairoPaintDevice pd(surface);
  327. runTests(&pd);
  328. /* Write output and clean up */
  329. cairo_surface_write_to_png (surface, "stroke.png");
  330. // cairo_destroy (cr);
  331. cairo_surface_destroy (surface);
  332. // Render a TQt3 native test image
  333. TQPixmap pm(500, 500);
  334. pm.fill(TQt::black);
  335. runTests(&pm);
  336. pm.save("tqt3.png", "PNG");
  337. return 0;
  338. }