ctkWidgetsUtils.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QBuffer>
  16. #include <QImage>
  17. #include <QGLWidget>
  18. #include <QPainter>
  19. #include <QWidget>
  20. // ctkWidgets includes
  21. #include "ctkWidgetsUtils.h"
  22. //----------------------------------------------------------------------------
  23. QString ctk::base64HTMLImageTagSrc(const QImage& image)
  24. {
  25. QByteArray byteArray;
  26. QBuffer buffer(&byteArray);
  27. buffer.open(QIODevice::WriteOnly);
  28. image.save(&buffer, "PNG");
  29. return QString("data:image/png;base64,%1")
  30. .arg(QString(buffer.data().toBase64()));
  31. }
  32. //----------------------------------------------------------------------------
  33. QImage ctk::grabWidget(QWidget* widget, QRect rectangle)
  34. {
  35. if (!widget)
  36. {
  37. return QImage();
  38. }
  39. if (!rectangle.isValid())
  40. {
  41. // Let Qt trigger layout mechanism and compute widget size.
  42. rectangle = QRect(0,0,-1,-1);
  43. }
  44. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  45. QPixmap widgetPixmap = QPixmap::grabWidget(widget, rectangle);
  46. #else
  47. QPixmap widgetPixmap = widget->grab(rectangle);
  48. #endif
  49. QImage widgetImage = widgetPixmap.toImage();
  50. QPainter painter;
  51. painter.begin(&widgetImage);
  52. foreach(QGLWidget* glWidget, widget->findChildren<QGLWidget*>())
  53. {
  54. if (!glWidget->isVisible())
  55. {
  56. continue;
  57. }
  58. QRect subWidgetRect = QRect(glWidget->mapTo(widget, QPoint(0,0)), glWidget->size());
  59. if (!rectangle.intersects(subWidgetRect))
  60. {
  61. continue;
  62. }
  63. QImage subImage = glWidget->grabFrameBuffer();
  64. painter.drawImage(subWidgetRect, subImage);
  65. }
  66. painter.end();
  67. return widgetImage;
  68. }
  69. //----------------------------------------------------------------------------
  70. QImage ctk::kwIconToQImage(const unsigned char *data, int width, int height, int pixelSize, unsigned int bufferLength, int options)
  71. {
  72. Q_UNUSED(options); // not yet supported
  73. QByteArray imageData = QByteArray::fromRawData(
  74. reinterpret_cast<const char *>(data), bufferLength);
  75. unsigned int expectedLength = width * height * pixelSize;
  76. // Start with a zlib header ? If not, then it must be base64 encoded
  77. if (bufferLength != expectedLength &&
  78. static_cast<unsigned char>(imageData[0]) != 0x78 &&
  79. static_cast<unsigned char>(imageData[1]) != 0xDA)
  80. {
  81. imageData = QByteArray::fromBase64(imageData);
  82. bufferLength = imageData.size();
  83. }
  84. if (bufferLength != expectedLength &&
  85. static_cast<unsigned char>(imageData[0]) == 0x78 &&
  86. static_cast<unsigned char>(imageData[1]) == 0xDA)
  87. {
  88. imageData.prepend(static_cast<char>((expectedLength >> 0) & 0xFF));
  89. imageData.prepend(static_cast<char>((expectedLength >> 8) & 0xFF));
  90. imageData.prepend(static_cast<char>((expectedLength >> 16) & 0xFF));
  91. imageData.prepend(static_cast<char>((expectedLength >> 24) & 0xFF));
  92. imageData = qUncompress(imageData);
  93. }
  94. QImage image(reinterpret_cast<unsigned char*>(imageData.data()),
  95. width, height, width * pixelSize,
  96. pixelSize == 4 ? QImage::Format_ARGB32 : QImage::Format_RGB888);
  97. if (pixelSize == 4)
  98. {
  99. return image.rgbSwapped();
  100. }
  101. return image.copy();
  102. }