summaryrefslogtreecommitdiffstats
path: root/quanta/dialogs/tagdialogs/tagimgdlg.cpp
blob: 17285d9682b347f68f3ca45813d10ae359c93cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
                          tagimgdlg.cpp  -  description
                             -------------------
    begin                : Sat Nov 27 1999
    copyright            : (C) 1999 by Yacovlev Alexander & Dmitry Poplavsky
                           (C) 2002, 2006 Andras Mantia
    email                : pdima@mail.univ.kiev.ua, amantia@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

//qt includes
#include <tqspinbox.h>
#include <tqimage.h>
//kde includes
#include <kapplication.h>
#include <klocale.h>
#include <kio/netaccess.h>
#include <kfiledialog.h>
#include <kurl.h>

//app includes
#include "pictureview.h"
#include "qextfileinfo.h"
#include "quantacommon.h"
#include "tagdialog.h"
#include "tagimgdlg.h"
#include "tagimgdlg.moc"


TagImgDlg::TagImgDlg(const DTDStruct *dtd, TQWidget *parent, const char *name)
  : TQWidget(parent,name), TagWidget(TQT_TQOBJECT(parent),name)
{
  m_dtd = dtd;
  img = 0L;
  isTmpFile = false;
  setCaption(name);
  initDialog();


  connect( buttonImgSource, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotFileSelect()) );
  connect( buttonRecalcImgSize, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotRecalcImgSize()) );
  connect( lineImgSource, TQT_SIGNAL( returnPressed()), this, TQT_SLOT(slotLineFileSelect()) );
}

TagImgDlg::~TagImgDlg()
{
  if (isTmpFile)
  {
    KIO::NetAccess::removeTempFile(imgFileName);
    isTmpFile = false;
  }
}

/** Choose new image */
void TagImgDlg::slotImageSet(const KURL& imageURL)
{
  if (isTmpFile)
  {
    KIO::NetAccess::removeTempFile(imgFileName);
    isTmpFile = false;
  }
  if (imageURL.isLocalFile())
  {
    imgFileName = imageURL.path();
  } else
  {
    KIO::NetAccess::download(imageURL, imgFileName, this); 
    isTmpFile = true; 
  }
  widgetImg->slotSetImage(imgFileName);
  
  img = new TQImage(imgFileName);
  if (!img->isNull() && lineWidth && lineHeight)
  {
    TQString s;
    s.setNum(img->width());
    lineWidth->setText(s);
    s.setNum(img->height());
    lineHeight->setText(s);
  }
}

/** recalculate image size */
void TagImgDlg::slotRecalcImgSize()
{
  if (img && lineWidth && lineHeight) {
    TQString s;
    s.setNum( img->width() );
    lineWidth->setText( s );
    s.setNum( img->height() );
    lineHeight->setText( s );
  }
}

/** select image */
void TagImgDlg::slotFileSelect()
{
//TODO: Implemented only for local files, but maybe it is enough
 KURL url = KFileDialog::getOpenURL( baseURL.url(), "*.gif *.jpg *.png *.jpeg *.bmp *.GIF *.JPG *.PNG *.JPEG *.BMP"+i18n("|Image Files\n*|All Files"));
 if ( !url.isEmpty() )
  {
    slotImageSet( url );
    url = QExtFileInfo::toRelative(url, baseURL);
    lineImgSource->setText( url.path() );
  }
}

void TagImgDlg::slotLineFileSelect()
{
  KURL url;
  QuantaCommon::setUrl(url, lineImgSource->text());
  slotImageSet(url);
}

void TagImgDlg::readAttributes( TQDict<TQString> *d )
{
  dict = d;

  updateDict("src",   lineImgSource );
  updateDict("alt",   lineAltText );
  if (lineWidth)
    updateDict("width", lineWidth );
  if (lineHeight)
    updateDict("height",lineHeight );
  if (lineHSpace)
    updateDict("hspace",lineHSpace );
  if (lineVSpace)
    updateDict("vspace",lineVSpace );
  if (comboAlign)
    updateDict("align", comboAlign );
  if (spinBorder)
    updateDict("border",spinBorder );
}

void TagImgDlg::writeAttributes( TQDict<TQString> *d )
{
  dict = d;
  TQString *t; // value of attr.

  if (( t=d->find("src") ))
  {
    lineImgSource->setText(*t);
    KURL url = baseURL;
    QuantaCommon::setUrl(url, *t);
    url = QExtFileInfo::toAbsolute(url, baseURL);
    slotImageSet( url );
  }
  if ( (t=d->find("alt")) )
    setValue(*t, lineAltText);
  if ( (t=d->find("width")) && lineWidth )
    setValue(*t, lineWidth);
  if ( (t=d->find("height")) && lineHeight )
    setValue(*t, lineHeight);
  if ( (t=d->find("hspace")) && lineHSpace)
    setValue(*t, lineHSpace);
  if ( (t=d->find("vspace")) && lineVSpace )
    setValue(*t, lineVSpace);
  if ( (t=d->find("align")) && comboAlign )
    setValue(*t, comboAlign);
  if ( (t=d->find("border")) && spinBorder )
    setValue(*t, spinBorder);
}