pictureitem.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "pictureitem.h"
  2. #include <QtDeclarative/qdeclarative.h>
  3. #include <QFile>
  4. #include <QDebug>
  5. #include <QUrl>
  6. #include <QString>
  7. #include <QImage>
  8. #include <QPainter>
  9. #include <QVariantMap>
  10. #include <QTimer>
  11. #include <QDir>
  12. #include <QDirIterator>
  13. #include <QApplication>
  14. #include <QDateTime>
  15. #include <QDocumentGallery>
  16. #include <QDesktopServices>
  17. #include <QSystemInfo>
  18. #include <QSystemStorageInfo>
  19. #include "drawthread.h"
  20. PictureItem::PictureItem(QDeclarativeItem *parent):
  21. QDeclarativeItem(parent),
  22. m_background(Qt::transparent),
  23. m_backgroundOld(Qt::transparent),
  24. m_thread(NULL)
  25. {
  26. // By default, QDeclarativeItem does not draw anything. If you subclass
  27. // QDeclarativeItem to create a visual item, you will need to uncomment the
  28. // following line:
  29. // setFlag(ItemHasNoContents, false);
  30. m_resolutions[QVGA] = QStringList() << QString::number(320) << QString::number(240);
  31. m_resolutions[VGA] = QStringList() << QString::number(640) << QString::number(480);
  32. m_resolutions[SVGA] = QStringList() << QString::number(800) << QString::number(600);
  33. m_resolutions[XGA] = QStringList() << QString::number(1024) << QString::number(768);
  34. m_resolutions[WXGA] = QStringList() << QString::number(1280) << QString::number(768);
  35. }
  36. PictureItem::~PictureItem()
  37. {
  38. qDebug() << __PRETTY_FUNCTION__;
  39. QDirIterator di(EMPTYIMAGE_PATH,QStringList() << "*.jpg",QDir::NoDotAndDotDot|QDir::Files);
  40. while(di.hasNext()){
  41. QString name = di.next();
  42. qDebug() << "removing: " << name;
  43. }
  44. }
  45. void PictureItem::createImage(ImageSizes imageSize, QVariantList imageList, QString fileName, QColor background, int margin, int aspectRatio)
  46. {
  47. qDebug() << __PRETTY_FUNCTION__ << " imageSize: " << imageSize << " filename: " << fileName << " background: " << background << " margin: " << margin;
  48. m_imageSize = imageSize;
  49. m_imageList = imageList;
  50. m_fileName = fileName;
  51. m_background = background;
  52. m_margin = margin;
  53. m_aspect = aspectRatio;
  54. if(m_thread)
  55. delete m_thread;
  56. m_thread = new DrawThread(m_picturesHome,m_resolutions);
  57. connect(m_thread,SIGNAL(finished()),this,SLOT(drawFinished()));
  58. m_thread->createImage(m_imageType,m_imageSize,m_imageList,m_fileName,m_background,m_backgroundOld,m_margin,m_aspect);
  59. }
  60. QStringList PictureItem::resolutionAsString(ImageSizes imageSize)
  61. {
  62. qDebug() << __PRETTY_FUNCTION__ << " imageSize: " << imageSize;
  63. return m_resolutions[imageSize];
  64. }
  65. QString PictureItem::imageSizeAsString(ImageSizes imageSize)
  66. {
  67. QString ret;
  68. switch(imageSize){
  69. case QVGA:{
  70. ret = "QVGA";
  71. }break;
  72. case VGA:{
  73. ret = "VGA";
  74. }break;
  75. case SVGA:{
  76. ret = "SVGA";
  77. }break;
  78. case XGA:{
  79. ret = "XGA";
  80. }break;
  81. case WXGA:{
  82. ret = "WXGA";
  83. }break;
  84. default:break;
  85. }
  86. return ret;
  87. }
  88. QString PictureItem::imageSizeWithResolutionAsString(ImageSizes imageSize)
  89. {
  90. qDebug() << __PRETTY_FUNCTION__;
  91. QString ret;
  92. switch(imageSize){
  93. case QVGA:{
  94. ret = "QVGA "+m_resolutions[QVGA].at(0)+"x"+m_resolutions[QVGA].at(1);
  95. }break;
  96. case VGA:{
  97. ret = "VGA "+m_resolutions[VGA].at(0)+"x"+m_resolutions[VGA].at(1);
  98. }break;
  99. case SVGA:{
  100. ret = "SVGA "+m_resolutions[SVGA].at(0)+"x"+m_resolutions[SVGA].at(1);
  101. }break;
  102. case XGA:{
  103. ret = "XGA "+m_resolutions[XGA].at(0)+"x"+m_resolutions[XGA].at(1);
  104. }break;
  105. case WXGA:{
  106. ret = "WXGA "+m_resolutions[WXGA].at(0)+"x"+m_resolutions[WXGA].at(1);
  107. }break;
  108. default:break;
  109. }
  110. qDebug() << "return: " << ret;
  111. return ret;
  112. }
  113. void PictureItem::paintEvent(QPaintEvent *event)
  114. {
  115. Q_UNUSED(event);
  116. }
  117. void PictureItem::setBackground(QColor backgroundColor)
  118. {
  119. qDebug() << __PRETTY_FUNCTION__;
  120. m_backgroundOld = m_background;
  121. m_background = backgroundColor;
  122. }
  123. QUrl PictureItem::emptyImage()
  124. {
  125. qDebug() << __PRETTY_FUNCTION__;
  126. bool back = false;
  127. bool type = false;
  128. if(m_background == m_backgroundOld)
  129. back = true;
  130. if(m_imageType == m_imageTypeOld)
  131. type = true;
  132. qDebug() << "back: " << back;
  133. qDebug() << "type: " << type;
  134. if(m_background != m_backgroundOld || m_imageType != m_imageTypeOld || m_emptyName.isEmpty()){
  135. QDir tempdir;
  136. if(tempdir.mkpath(EMPTYIMAGE_PATH))
  137. qDebug() << "dir created";
  138. else
  139. qDebug() << "dir not created";
  140. if(QFile::remove(m_emptyName))
  141. qDebug() << "remove success";
  142. else
  143. qDebug() << "remove failed";
  144. m_emptyName = EMPTYIMAGE_PATH+QString::number(qrand())+".jpg";
  145. qDebug() << "emptyname: " << m_emptyName;
  146. QImage* img = new QImage(QSize(1280,768),IMAGE_FORMAT);
  147. QPainter* combiner = new QPainter(img);
  148. combiner->fillRect(QRect(0,0,img->width(),img->height()),m_background);
  149. bool save = img->save(m_emptyName);
  150. if(img->isNull())
  151. qDebug() << "empty image is null";
  152. if(!save)
  153. qDebug() << "cannot create empty image";
  154. m_backgroundOld = m_background;
  155. delete combiner;
  156. delete img;
  157. }
  158. return QUrl::fromLocalFile(m_emptyName);
  159. }
  160. QString PictureItem::freeFilename()
  161. {
  162. qDebug() << __PRETTY_FUNCTION__;
  163. QString fn;
  164. int count=0;
  165. int subcount = 0;
  166. QString date = QDate::currentDate().toString("ddMMyyyy");
  167. date = APP_NAME_SHORT+date;
  168. QDirIterator di(m_picturesHome,QStringList() << "*.jpg",QDir::Files);
  169. while(di.hasNext()){
  170. QString name = di.next();
  171. QFileInfo fi(name);
  172. name = fi.baseName();
  173. if(QString::compare(date,name,Qt::CaseInsensitive)==0)
  174. count++;
  175. QStringList list = name.split('_');
  176. if(list.count()>1){
  177. if(QString::compare(date,list.at(0),Qt::CaseInsensitive)==0){
  178. if(subcount<list.at(1).toInt())
  179. subcount = list.at(1).toInt();
  180. }
  181. }
  182. }
  183. if(subcount && !count)
  184. count = 1;
  185. count+=subcount;
  186. if(count)
  187. fn = date+"_"+QString::number(count)+".jpg";
  188. else
  189. fn = date+".jpg";
  190. return fn;
  191. }
  192. void PictureItem::drawFinished()
  193. {
  194. qDebug() << __PRETTY_FUNCTION__;
  195. bool success = m_thread->success();
  196. bool stopped = m_thread->stopRequested();
  197. m_currentImage = QUrl::fromLocalFile(m_thread->filename());
  198. delete m_thread;
  199. m_thread = NULL;
  200. if(!stopped){
  201. emit imageFinished(success);
  202. }else{
  203. m_currentImage.clear();
  204. emit canceled();
  205. }
  206. }
  207. void PictureItem::cancelDrawer()
  208. {
  209. qDebug() << __PRETTY_FUNCTION__;
  210. if(m_thread){
  211. if(m_thread->isRunning()){
  212. m_thread->stopWorking();
  213. }
  214. }
  215. }
  216. PictureItem::DriveStorage PictureItem::storage()
  217. {
  218. qDebug() << __PRETTY_FUNCTION__;
  219. return m_storage;
  220. }
  221. void PictureItem::setStorage(DriveStorage storage)
  222. {
  223. qDebug() << __PRETTY_FUNCTION__ << " " << storage;
  224. QtMobility::QSystemStorageInfo sysInfo;
  225. QStringList drives = sysInfo.logicalDrives();
  226. qDebug() << "drives: " <<drives;
  227. switch(storage){
  228. case StorageInternal:{
  229. m_picturesHome = QString::null;
  230. QString path(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
  231. if(drives.contains("e",Qt::CaseInsensitive)){
  232. int ind = drives.indexOf(QRegExp("e",Qt::CaseInsensitive));
  233. qDebug() << "ind: " << ind;
  234. if(ind > -1){
  235. QString id = drives[ind];
  236. qDebug() << "drive: " << id << " type: " << sysInfo.typeForDrive(id);
  237. if(QtMobility::QSystemStorageInfo::InternalDrive == sysInfo.typeForDrive(id) ||
  238. QtMobility::QSystemStorageInfo::RemovableDrive == sysInfo.typeForDrive(id)){
  239. if(sysInfo.availableDiskSpace(id)>0){
  240. m_picturesHome = id;
  241. QStringList splitted = path.split('/',QString::SkipEmptyParts);
  242. path = ":/"+splitted.last();
  243. }
  244. }
  245. }
  246. }
  247. if(m_picturesHome.isEmpty()){
  248. m_picturesHome="C";
  249. path = path.mid(1);
  250. }
  251. m_storage = storage;
  252. m_picturesHome = m_picturesHome.append(path);
  253. }break;
  254. case StorageRemovable:{
  255. QString newHome;
  256. foreach(QString drive, drives){
  257. qDebug() << "drive: " << drive << " type: " << sysInfo.typeForDrive(drive);
  258. if(QtMobility::QSystemStorageInfo::RemovableDrive == sysInfo.typeForDrive(drive)){
  259. newHome = drive;
  260. m_storage = storage;
  261. }
  262. }
  263. if(newHome.isEmpty()){
  264. m_storage = StorageError;
  265. }else{
  266. m_picturesHome = newHome;
  267. QString tmp(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
  268. QStringList splitted = tmp.split('/',QString::SkipEmptyParts);
  269. m_picturesHome = m_picturesHome.append(":/"+splitted.last());
  270. }
  271. }break;
  272. case Gallery:{
  273. m_picturesHome = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
  274. m_storage = storage;
  275. }break;
  276. default:
  277. m_storage = StorageError;
  278. break;
  279. }
  280. if(!m_picturesHome.isEmpty() && !m_picturesHome.endsWith('/'))
  281. m_picturesHome = m_picturesHome.append("/");
  282. qDebug() << "Save path: " << m_picturesHome;
  283. qDebug() << "Storage state: " << m_storage;
  284. emit storageChanged(m_storage);
  285. }