TDE graphics utilities
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.

280 lines
7.2KB

  1. /*
  2. TDE Icon Editor - a small graphics drawing program for the TDE
  3. Copyright (C) 1998 Thomas Tanghus (tanghus@kde.org)
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <stdlib.h>
  18. #include <tqimage.h>
  19. #include <kmessagebox.h>
  20. #include <kfiledialog.h>
  21. #include <kio/netaccess.h>
  22. #include <kimageio.h>
  23. #include <kdebug.h>
  24. #include <klocale.h>
  25. #include "kicon.h"
  26. #include "utils.h"
  27. KIconEditIcon::KIconEditIcon(TQObject *parent, const TQImage *img, KURL url)
  28. : TQObject(parent)
  29. {
  30. f = 0;
  31. _lastdir = "/";
  32. //checkUnNamedBackup(img);
  33. if(!url.isEmpty())
  34. {
  35. open(img, url);
  36. }
  37. }
  38. KIconEditIcon::~KIconEditIcon()
  39. {
  40. }
  41. bool KIconEditIcon::open(const TQImage *image, KURL url)
  42. {
  43. TQImage *img = (TQImage*)image;
  44. if(url.isEmpty())
  45. return false;
  46. kdDebug(4640) << "KIconEditIcon::open " << url.prettyURL() << endl;
  47. if(!url.isValid()) // try to see if it is a relative filename
  48. {
  49. kdDebug(4640) << "KIconEditIcon::open (malformed) " << url.prettyURL() << endl;
  50. TQFileInfo fi(url.url());
  51. if(fi.isRelative())
  52. url = "file:" + fi.absFilePath();
  53. if(!url.isValid()) // Giving up
  54. {
  55. TQString msg = i18n("The URL: %1 \nseems to be malformed.\n").arg(url.url());
  56. KMessageBox::sorry((TQWidget*)parent(), msg);
  57. return false;
  58. }
  59. }
  60. TQString filename;
  61. if(url.isLocalFile())
  62. {
  63. filename = url.path();
  64. }
  65. else
  66. {
  67. if(!TDEIO::NetAccess::download( url, filename, (TQWidget*)parent() ))
  68. {
  69. TQString msg = i18n("There was an error loading:\n%1\n").arg(url.prettyURL());
  70. KMessageBox::error((TQWidget*)parent(), msg);
  71. return false;
  72. }
  73. }
  74. bool loadedOk = img->load(filename);
  75. if(!url.isLocalFile())
  76. {
  77. TDEIO::NetAccess::removeTempFile( filename );
  78. }
  79. if(!loadedOk)
  80. {
  81. TQString msg = i18n("There was an error loading:\n%1\n").arg(url.prettyURL());
  82. KMessageBox::error((TQWidget*)parent(), msg);
  83. }
  84. else
  85. {
  86. kdDebug(4640) << "KIconEditIcon::open - Image loaded" << endl;
  87. // _url is saved off for use in saving the image to the same
  88. // file later - should include full path
  89. if(url.isLocalFile())
  90. {
  91. _url = url.path();
  92. }
  93. else
  94. {
  95. _url = "";
  96. }
  97. kdDebug(4640) << "KIcon: _url: " << _url << endl;
  98. // this causes updates of preview, color palettes, etc.
  99. emit loaded(img);
  100. kdDebug(4640) << "loaded(img)" << endl;
  101. // this is the name that shows up in status bar -
  102. // should be filename with path
  103. emit newname(url.prettyURL());
  104. kdDebug(4640) << "newname(_url) : " << url.prettyURL() << endl;
  105. emit addrecent(url.prettyURL());
  106. kdDebug(4640) << "KIconEditIcon::open - done" << endl;
  107. }
  108. return loadedOk;
  109. }
  110. bool KIconEditIcon::promptForFile(const TQImage *img)
  111. {
  112. kdDebug(4640) << "KIconEditIcon::promptForFile(const TQImage *img)" << endl;
  113. /*
  114. TQString filter = i18n("*|All Files (*)\n"
  115. "*.xpm|XPM (*.xpm)\n"
  116. "*.png|PNG (*.png)\n"
  117. "*.gif|GIF files (*.gif)\n"
  118. "*.jpg|JPEG files (*.jpg)\n"
  119. "*.ico|Icon files (*.ico)\n");
  120. KURL url = KFileDialog::getOpenURL( TQString(), filter );
  121. */
  122. bool loaded = false;
  123. KURL url = KFileDialog::getImageOpenURL( TQString(), TQT_TQWIDGET(parent()) );
  124. if( !url.isEmpty() )
  125. {
  126. loaded = open( img, url );
  127. }
  128. return loaded;
  129. }
  130. bool KIconEditIcon::saveAs(const TQImage *image)
  131. {
  132. kdDebug(4640) << "KIconEditIcon::saveAs" << endl;
  133. TQString file;
  134. //Get list of file types..
  135. KFileDialog *dialog=new KFileDialog(TQString(), TQString(), TQT_TQWIDGET(parent()), "file dialog", true);
  136. dialog->setCaption( i18n("Save Icon As") );
  137. dialog->setKeepLocation( true );
  138. dialog->setMimeFilter( KImageIO::mimeTypes(KImageIO::Writing), "image/png" );
  139. dialog->setOperationMode( KFileDialog::Saving );
  140. if(dialog->exec()==TQDialog::Accepted)
  141. {
  142. file = dialog->selectedFile();
  143. if( file.isNull() )
  144. {
  145. delete dialog;
  146. return false;
  147. }
  148. if ( !KImageIO::canWrite(KImageIO::type(file)) )
  149. {
  150. if ( KImageIO::canWrite(KImageIO::typeForMime(dialog->currentFilter())) )
  151. file += "."+KImageIO::suffix(KImageIO::typeForMime(dialog->currentFilter()));
  152. else
  153. file += ".png";
  154. }
  155. }
  156. else
  157. {
  158. delete dialog;
  159. return false;
  160. }
  161. delete dialog;
  162. if(TQFile::exists(file))
  163. {
  164. int r=KMessageBox::warningContinueCancel(TQT_TQWIDGET(parent()),
  165. i18n( "A file named \"%1\" already exists. "
  166. "Overwrite it?" ).arg(file),
  167. i18n( "Overwrite File?" ),
  168. i18n( "&Overwrite" ) );
  169. if(r==KMessageBox::Cancel)
  170. {
  171. return false;
  172. }
  173. }
  174. return save( image, file );
  175. }
  176. bool KIconEditIcon::save(const TQImage *image, const TQString &_filename)
  177. {
  178. kdDebug(4640) << "KIconEditIcon::save" << endl;
  179. TQString filename = _filename;
  180. if(filename.isEmpty())
  181. {
  182. if(_url.isEmpty())
  183. {
  184. return saveAs(image);
  185. }
  186. else
  187. {
  188. KURL turl(_url);
  189. filename = turl.path();
  190. }
  191. }
  192. TQImage *img = (TQImage*)image;
  193. img->setAlphaBuffer(true);
  194. KURL turl(filename);
  195. TQString str = turl.path();
  196. bool savedOk = false;
  197. /* base image type on file extension - let kimageio
  198. take care of this determination */
  199. if(img->save(str, KImageIO::type(str).ascii()))
  200. {
  201. kdDebug(4640) << "img->save()) successful" << endl;
  202. emit saved();
  203. _url = filename;
  204. // emit signal to change title bar to reflect new name
  205. emit newname(filename);
  206. kdDebug(4640) << "newname(filenamme) : " << _url << endl;
  207. savedOk = true;
  208. emit addrecent(filename);
  209. }
  210. else
  211. {
  212. TQString msg = i18n("There was an error saving:\n%1\n").arg(str);
  213. KMessageBox::error((TQWidget*)parent(), msg);
  214. kdDebug(4640) << "KIconEditIcon::save - " << msg << endl;
  215. }
  216. kdDebug(4640) << "KIconEditIcon::save - done" << endl;
  217. return savedOk;
  218. }
  219. #include "kicon.moc"