Subversion Repositories Scribus

Rev

Rev 5821 | Rev 5835 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
3 paul 7
/***************************************************************************
8
                          picstatus.cpp  -  description
9
                             -------------------
10
    begin                : Fri Nov 29 2001
11
    copyright            : (C) 2001 by Franz Schmid
12
    email                : Franz.Schmid@altmuehlnet.de
13
 ***************************************************************************/
14
 
15
/***************************************************************************
16
 *                                                                         *
17
 *   This program is free software; you can redistribute it and/or modify  *
18
 *   it under the terms of the GNU General Public License as published by  *
19
 *   the Free Software Foundation; either version 2 of the License, or     *
20
 *   (at your option) any later version.                                   *
21
 *                                                                         *
22
 ***************************************************************************/
23
 
24
#include "picstatus.h"
25
#include "picstatus.moc"
26
#include <qfileinfo.h>
27
#include <qfiledialog.h>
3698 cbradney 28
#include <qmessagebox.h>
3 paul 29
#include <qtoolbutton.h>
30
#include <qstringlist.h>
31
#include <qtextstream.h>
5834 cbradney 32
#include <qtooltip.h>
83 Franz 33
#include <qcursor.h>
3 paul 34
#include <cstdio>
35
#include "picsearch.h"
838 cbradney 36
#include "scribusdoc.h"
456 fschmid 37
#include "pageitem.h"
2805 craig 38
#include "filesearch.h"
5781 cbradney 39
#include "scribuscore.h"
3 paul 40
extern QPixmap loadIcon(QString nam);
41
 
3205 craig 42
 
5251 fschmid 43
const unsigned short PicStatus::COL_PREVIEW = 0;
44
const unsigned short PicStatus::COL_FILENAME = 1;
45
const unsigned short PicStatus::COL_PATH = 2;
46
const unsigned short PicStatus::COL_PAGE = 3;
47
const unsigned short PicStatus::COL_GOTO = 4;
48
const unsigned short PicStatus::COL_PRINT = 5;
49
const unsigned short PicStatus::COL_STATUS = 6;
50
const unsigned short PicStatus::COL_SEARCH = 7;
2807 craig 51
 
3367 cbradney 52
QString PicStatus::trOK = "";
53
QString PicStatus::trMissing = "";
54
QString PicStatus::trSearch = "";
55
QString PicStatus::trCancelSearch = "";
56
QString PicStatus::trGoto = "";
2807 craig 57
 
145 Franz 58
 
5821 cbradney 59
PicStatus::PicStatus(QWidget* parent, ScribusDoc *docu) :
2808 craig 60
        QDialog( parent, "pic", true, 0 )
3 paul 61
{
3367 cbradney 62
        languageChange();
282 Franz 63
        uint p, i;
64
        QString tmp;
2807 craig 65
        setCaption( tr( "Manage Pictures" ) );
282 Franz 66
        setIcon(loadIcon("AppIcon.png"));
5821 cbradney 67
        m_Doc = docu;
282 Franz 68
        ItemNrs.clear();
69
        FlagsPic.clear();
70
        PicStatusLayout = new QVBoxLayout( this );
71
        PicStatusLayout->setSpacing( 6 );
72
        PicStatusLayout->setMargin( 11 );
3 paul 73
 
282 Franz 74
        PicTable = new QTable( this, "PicTable" );
75
        PicTable->setLeftMargin(0);
76
        PicTable->verticalHeader()->hide();
5251 fschmid 77
        PicTable->setNumCols( 8 );
282 Franz 78
        Header = PicTable->horizontalHeader();
5327 cbradney 79
        QString tmpc[] = { "", tr("Name"),  tr("Path"),  tr("Page"), " ",  tr("Print"),  tr("Status"), " "};
167 Franz 80
        size_t ar = sizeof(tmpc) / sizeof(*tmpc);
81
        for (uint a = 0; a < ar; ++a)
253 Franz 82
                Header->setLabel(a, tmpc[a]);
282 Franz 83
        Zeilen = 0;
5821 cbradney 84
        for (i=0; i < m_Doc->MasterItems.count(); ++i)
282 Franz 85
        {
5821 cbradney 86
                if (m_Doc->MasterItems.at(i)->itemType() == PageItem::ImageFrame)
282 Franz 87
                {
456 fschmid 88
                        Zeilen++;
89
                        ItemNrs.append(i);
282 Franz 90
                }
91
        }
5821 cbradney 92
        for (i=0; i<m_Doc->Items->count(); ++i)
282 Franz 93
        {
5821 cbradney 94
                if (m_Doc->Items->at(i)->itemType() == PageItem::ImageFrame)
282 Franz 95
                {
456 fschmid 96
                        Zeilen++;
97
                        ItemNrs.append(i);
282 Franz 98
                }
99
        }
161 Franz 100
        PicTable->setNumRows(Zeilen);
282 Franz 101
        int Zeilen2 = 0;
5821 cbradney 102
        for (i=0; i < m_Doc->MasterItems.count(); ++i)
282 Franz 103
        {
5821 cbradney 104
                if (m_Doc->MasterItems.at(i)->itemType() == PageItem::ImageFrame)
282 Franz 105
                {
5821 cbradney 106
                        QFileInfo fi = QFileInfo(m_Doc->MasterItems.at(i)->Pfile);
2807 craig 107
                        PicTable->setText(Zeilen2, COL_FILENAME, fi.fileName());
108
                        PicTable->setText(Zeilen2, COL_PATH, fi.dirPath());
5821 cbradney 109
                        PicTable->setText(Zeilen2, COL_PAGE, m_Doc->MasterItems.at(i)->OnMasterPage);
456 fschmid 110
                        QToolButton *tb2 = new QToolButton(this, tmp.setNum(Zeilen2));
2807 craig 111
                        tb2->setText( trGoto);
456 fschmid 112
                        tb2->setEraseColor(white);
2807 craig 113
                        PicTable->setColumnWidth(COL_GOTO, tb2->fontMetrics().width( trGoto)+10);
114
                        PicTable->setCellWidget(Zeilen2, COL_GOTO, tb2);
456 fschmid 115
                        connect(tb2, SIGNAL(clicked()), this, SLOT(GotoPic()));
116
                        QCheckBox *cp2 = new QCheckBox(this, tmp.setNum(Zeilen2));
117
                        cp2->setText( tr("Yes"));
5821 cbradney 118
                        cp2->setChecked(m_Doc->MasterItems.at(i)->printEnabled());
456 fschmid 119
                        cp2->setEraseColor(white);
120
                        FlagsPic.append(cp2);
2807 craig 121
                        PicTable->setCellWidget(Zeilen2, COL_PRINT, cp2);
456 fschmid 122
                        connect(cp2, SIGNAL(clicked()), this, SLOT(PrintPic()));
5821 cbradney 123
                        if (m_Doc->MasterItems.at(i)->PicAvail)
5251 fschmid 124
                        {
125
                                QPixmap pm;
5821 cbradney 126
                                pm.convertFromImage(m_Doc->MasterItems.at(i)->pixm.smoothScale(64, 64, QImage::ScaleMin));
5251 fschmid 127
                                PicTable->setPixmap(Zeilen2, COL_PREVIEW, pm);
2807 craig 128
                                PicTable->setText(Zeilen2, COL_STATUS, trOK);
5251 fschmid 129
                        }
456 fschmid 130
                        else
2807 craig 131
                                PicTable->setText(Zeilen2, COL_STATUS, trMissing);
456 fschmid 132
                        QToolButton *tb = new QToolButton(this, tmp.setNum(Zeilen2));
2807 craig 133
                        tb->setText( trSearch);
134
                        PicTable->setColumnWidth(COL_SEARCH, tb->fontMetrics().width( trCancelSearch)+10);
456 fschmid 135
                        tb->setEraseColor(white);
2807 craig 136
                        PicTable->setCellWidget(Zeilen2, COL_SEARCH, tb);
456 fschmid 137
                        connect(tb, SIGNAL(clicked()), this, SLOT(SearchPic()));
5251 fschmid 138
                        PicTable->adjustRow(Zeilen2);
456 fschmid 139
                        Zeilen2++;
282 Franz 140
                }
141
        }
5821 cbradney 142
        for (i=0; i< m_Doc->Items->count(); ++i)
282 Franz 143
        {
5821 cbradney 144
                if (m_Doc->Items->at(i)->itemType() == PageItem::ImageFrame)
282 Franz 145
                {
5821 cbradney 146
                        QFileInfo fi = QFileInfo(m_Doc->Items->at(i)->Pfile);
2807 craig 147
                        PicTable->setText(Zeilen2, COL_FILENAME, fi.fileName());
148
                        PicTable->setText(Zeilen2, COL_PATH, fi.dirPath());
5821 cbradney 149
                        p = m_Doc->Items->at(i)->OwnPage;
2807 craig 150
                        PicTable->setText(Zeilen2, COL_PAGE, tmp.setNum(p+1));
456 fschmid 151
                        QToolButton *tb2 = new QToolButton(this, tmp.setNum(Zeilen2));
2807 craig 152
                        tb2->setText( trGoto);
456 fschmid 153
                        tb2->setEraseColor(white);
2807 craig 154
                        PicTable->setColumnWidth(COL_GOTO, tb2->fontMetrics().width( trGoto)+10);
155
                        PicTable->setCellWidget(Zeilen2, COL_GOTO, tb2);
456 fschmid 156
                        connect(tb2, SIGNAL(clicked()), this, SLOT(GotoPic()));
157
                        QCheckBox *cp2 = new QCheckBox(this, tmp.setNum(Zeilen2));
158
                        cp2->setText( tr("Yes"));
5821 cbradney 159
                        cp2->setChecked(m_Doc->Items->at(i)->printEnabled());
456 fschmid 160
                        cp2->setEraseColor(white);
161
                        FlagsPic.append(cp2);
2807 craig 162
                        PicTable->setCellWidget(Zeilen2, COL_PRINT, cp2);
456 fschmid 163
                        connect(cp2, SIGNAL(clicked()), this, SLOT(PrintPic()));
5821 cbradney 164
                        if (m_Doc->Items->at(i)->PicAvail)
5251 fschmid 165
                        {
166
                                QPixmap pm;
5821 cbradney 167
                                pm.convertFromImage(m_Doc->Items->at(i)->pixm.smoothScale(64, 64, QImage::ScaleMin));
5251 fschmid 168
                                PicTable->setPixmap(Zeilen2, COL_PREVIEW, pm);
2807 craig 169
                                PicTable->setText(Zeilen2, COL_STATUS, trOK);
5251 fschmid 170
                        }
456 fschmid 171
                        else
2807 craig 172
                                PicTable->setText(Zeilen2, COL_STATUS, trMissing);
456 fschmid 173
                        QToolButton *tb = new QToolButton(this, tmp.setNum(Zeilen2));
2807 craig 174
                        tb->setText( trSearch);
175
                        PicTable->setColumnWidth(COL_SEARCH, tb->fontMetrics().width( trCancelSearch)+10);
456 fschmid 176
                        tb->setEraseColor(white);
2807 craig 177
                        PicTable->setCellWidget(Zeilen2, COL_SEARCH, tb);
456 fschmid 178
                        connect(tb, SIGNAL(clicked()), this, SLOT(SearchPic()));
5251 fschmid 179
                        PicTable->adjustRow(Zeilen2);
456 fschmid 180
                        Zeilen2++;
282 Franz 181
                }
182
        }
161 Franz 183
 
5251 fschmid 184
        PicTable->adjustColumn(COL_PREVIEW);
2807 craig 185
        PicTable->adjustColumn(COL_FILENAME);
186
        PicTable->adjustColumn(COL_PATH);
187
        PicTable->adjustColumn(COL_PAGE);
188
        PicTable->adjustColumn(COL_PRINT);
189
        PicTable->adjustColumn(COL_STATUS);
282 Franz 190
        PicTable->setSorting(false);
5834 cbradney 191
        PicTable->setSelectionMode(QTable::MultiRow);
282 Franz 192
        PicTable->setColumnMovingEnabled(false);
193
        PicTable->setRowMovingEnabled(false);
194
        Header->setMovingEnabled(false);
5251 fschmid 195
        PicTable->setColumnReadOnly(COL_PREVIEW, true);
2807 craig 196
        PicTable->setColumnReadOnly(COL_FILENAME, true);
197
        PicTable->setColumnReadOnly(COL_PATH, true);
198
        PicTable->setColumnReadOnly(COL_PAGE, true);
199
        PicTable->setColumnReadOnly(COL_STATUS, true);
282 Franz 200
        PicStatusLayout->addWidget( PicTable );
3 paul 201
 
282 Franz 202
        Layout2 = new QHBoxLayout;
2807 craig 203
        Layout2->setSpacing( 3 );
282 Franz 204
        Layout2->setMargin( 0 );
205
        QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
206
        Layout2->addItem( spacer );
5834 cbradney 207
        searchAllButton = new QPushButton( tr( "Set New Location" ), this, "searchAllButton" );
208
        Layout2->addWidget( searchAllButton );
209
        okButton = new QPushButton( tr( "Close" ), this, "okButton" );
210
        Layout2->addWidget( okButton );
282 Franz 211
        PicStatusLayout->addLayout( Layout2 );
3 paul 212
 
282 Franz 213
        // signals and slots connections
5834 cbradney 214
        connect( searchAllButton, SIGNAL( clicked() ), this, SLOT( searchAllPics() ) );
215
        connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
216
 
217
        QToolTip::add( searchAllButton, "<qt>" + tr( "Set a new location for the selected items. Useful when you may have moved the document but not the images." ) + "</qt>");
3 paul 218
}
219
 
3367 cbradney 220
void PicStatus::languageChange()
221
{
222
        PicStatus::trOK = tr("OK");
223
        PicStatus::trMissing = tr("Missing");
224
        PicStatus::trSearch = tr("Search");
225
        PicStatus::trCancelSearch = tr("Cancel Search");
226
        PicStatus::trGoto = tr("Goto");
227
}
228
 
3 paul 229
void PicStatus::GotoPic()
230
{
5821 cbradney 231
        QString pageText(PicTable->text(QString(sender()->name()).toInt(), COL_PAGE));
1615 fschmid 232
        bool ok = false;
233
        int pageNum = pageText.toInt(&ok);
5781 cbradney 234
        ScCore->primaryMainWindow()->closeActiveWindowMasterPageEditor();
1615 fschmid 235
        if (!ok)
2093 cbradney 236
                emit selectMasterPage(pageText);
1615 fschmid 237
        else
238
                emit selectPage(pageNum-1);
3 paul 239
}
240
 
241
void PicStatus::SearchPic()
242
{
2805 craig 243
        // FIXME: This is a pretty ugly hack IMO - carried over from the old
244
        // SearchPic. Table handling needs work.
2807 craig 245
        unsigned int row = QString(sender()->name()).toUInt();
5251 fschmid 246
        QString fileName = PicTable->text(row, COL_FILENAME);
5834 cbradney 247
        QString workDir;
4096 cbradney 248
#ifndef _WIN32
249
        workDir = QDir::homeDirPath();
250
#endif
251
        // Pictures may be located completely outside home or documents directory
252
        // so ask base search directory first
5834 cbradney 253
        QString searchBase;
4096 cbradney 254
        searchBase = QFileDialog::getExistingDirectory( workDir, NULL, NULL, tr("Select a base directory for search"));
255
        if( searchBase.isEmpty() || !QDir().exists(searchBase) )
256
                return;
2805 craig 257
        // Set up the search, then return to the event loop until it notifies us
258
        // that it's done.
2808 craig 259
        // Note: search will be deleted when this PicStatus is, so there's no
2805 craig 260
        // need to worry about cleanup.
4096 cbradney 261
        FileSearch* search = new FileSearch(this, fileName, searchBase);
2808 craig 262
        Q_CHECK_PTR(search);
263
        connect(search,
2805 craig 264
                        SIGNAL(searchComplete(const QStringList&, const QString&)),
265
                        SLOT(SearchPicFinished(const QStringList&, const QString&)));
2927 craig 266
        connect(search, SIGNAL(aborted(bool)), SLOT(SearchPicAborted(bool)));
2807 craig 267
        // Set up the UI to let the user cancel the search, then start it
2808 craig 268
        setSearchButton(row, true, search);
269
        search->start();
2805 craig 270
}
271
 
2807 craig 272
void PicStatus::SearchPicAborted(bool userCancelled)
2805 craig 273
{
2808 craig 274
        const FileSearch* search = dynamic_cast<const FileSearch*>(sender());
275
        Q_ASSERT(search);
2807 craig 276
        // Restore button to normal "search"
2808 craig 277
        unsigned int row = getRowByFileName(search->fileName());
278
        setSearchButton(row, false, search);
2807 craig 279
        // and inform user if it it wasn't them who asked to stop it.
280
        if (!userCancelled)
281
                // A running search failed
282
                QMessageBox::warning(this, tr("Scribus - Image Search"),
2927 craig 283
                                tr("The search failed: %1").arg(search->lastError()),
2807 craig 284
                                QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
285
                                QMessageBox::NoButton);
2805 craig 286
}
287
 
288
void PicStatus::SearchPicFinished(const QStringList & matches, const QString & fileName)
289
{
2808 craig 290
        const FileSearch* search = dynamic_cast<const FileSearch*>(sender());
291
        Q_ASSERT(search);
2805 craig 292
        // First, find out what row the search result is about
2808 craig 293
        unsigned int row = getRowByFileName(fileName);
2807 craig 294
        // Restore the gui to "search"
2808 craig 295
        setSearchButton(row, false, search);
2807 craig 296
        // Then display a dialog for the user to select images from
297
        // if one or more were found
2808 craig 298
        if (matches.count() == 0)
161 Franz 299
        {
2808 craig 300
                QMessageBox::information(this, tr("Scribus - Image Search"),
301
                                tr("No images named \"%1\" were found.").arg(fileName),
302
                                QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
303
                                QMessageBox::NoButton);
161 Franz 304
        }
3 paul 305
        else
161 Franz 306
        {
2808 craig 307
                PicSearch *dia = new PicSearch(this, fileName, matches);
308
                if (dia->exec())
3 paul 309
                {
2809 craig 310
                        Q_ASSERT(!dia->Bild.isEmpty());
311
                        loadPictByRow(dia->Bild, row);
5821 cbradney 312
                        m_Doc->view()->DrawNew();
3 paul 313
                }
2808 craig 314
                delete dia;
161 Franz 315
        }
3 paul 316
}
317
 
2809 craig 318
bool PicStatus::loadPictByRow(const QString & newFilePath, unsigned int row)
319
{
320
        unsigned int itemNumber = ItemNrs[row];
321
        // FIXME: error checking
5821 cbradney 322
        m_Doc->LoadPict(newFilePath, itemNumber);
2809 craig 323
        // WTF?
324
        bool isMaster = PicTable->cellWidget(row, COL_GOTO)->isEnabled();
5821 cbradney 325
        PageItem* item = isMaster ? m_Doc->DocItems.at(itemNumber) : m_Doc->MasterItems.at(itemNumber);
2809 craig 326
        // Set missing flag again. Since we do no error checking of the load,
327
        // missing will generally mean "failed to load".
328
        PicTable->setText(row, COL_STATUS, item->PicAvail ? trOK : trMissing);
329
        PicTable->setText(row, COL_PATH, QFileInfo(newFilePath).dirPath(true));
5251 fschmid 330
        if (item->PicAvail)
331
        {
332
                QPixmap pm;
333
                pm.convertFromImage(item->pixm.smoothScale(64, 64, QImage::ScaleMin));
334
                PicTable->setPixmap(row, COL_PREVIEW, pm);
335
                PicTable->adjustRow(row);
336
        }
2809 craig 337
        return item->PicAvail;
338
}
339
 
2808 craig 340
void PicStatus::setSearchButton(int row, bool toCancel, const FileSearch* searcher)
2807 craig 341
{
342
        QWidget* item = PicTable->cellWidget(row, COL_SEARCH);
343
        Q_ASSERT(item);
344
        QToolButton* tb = dynamic_cast<QToolButton*>(item);
345
        Q_ASSERT(tb);
346
        if (toCancel)
347
        {
348
                tb->setText( trCancelSearch );
349
                disconnect(tb, SIGNAL(clicked()), this, SLOT(SearchPic()));
350
                connect(tb, SIGNAL(clicked()), searcher, SLOT(cancel()));
351
        }
352
        else
353
        {
354
                tb->setText(trSearch);
355
                disconnect(tb, SIGNAL(clicked()), searcher, SLOT(cancel()));
356
                connect(tb, SIGNAL(clicked()), SLOT(SearchPic()));
357
        }
358
}
359
 
360
int PicStatus::getRowByFileName(const QString & fileName)
361
{
362
        bool found = false;
363
        unsigned int row;
364
        // This cast is OK since numRows must logically return >=0
365
        for (row = 0; row < static_cast<unsigned int>(PicTable->numRows()); ++row)
366
        {
5251 fschmid 367
                if ( PicTable->text(row, COL_FILENAME) == fileName )
2807 craig 368
                {
369
                        found = true;
370
                        break;
371
                }
372
        }
373
        Q_ASSERT(found);
374
        return row;
375
}
376
 
3 paul 377
void PicStatus::PrintPic()
378
{
379
        uint ZNr = QString(sender()->name()).toUInt();
380
        uint ItNr = ItemNrs[ZNr];
456 fschmid 381
//      uint PgNr = PicTable->text(ZNr, 2).toInt()-1;
5251 fschmid 382
        if (PicTable->cellWidget(ZNr, COL_PRINT)->isEnabled())
5821 cbradney 383
                m_Doc->DocItems.at(ItNr)->setPrintEnabled(FlagsPic.at(ZNr)->isChecked());
3 paul 384
        else
5821 cbradney 385
                m_Doc->MasterItems.at(ItNr)->setPrintEnabled(FlagsPic.at(ZNr)->isChecked());
3 paul 386
}
5834 cbradney 387
 
388
void PicStatus::searchAllPics( )
389
{
390
        QString workDir;
391
#ifndef _WIN32
392
        workDir = QDir::homeDirPath();
393
#endif
394
        QString searchBase = QFileDialog::getExistingDirectory( workDir, NULL, NULL, tr("Select a base directory for your selected rows"));
395
        if( searchBase.isEmpty() || !QDir().exists(searchBase) )
396
                return;
397
        uint updated=0;
398
        for (int i=0; i<PicTable->numRows(); ++i)
399
        {
400
                if (PicTable->isRowSelected(i))
401
                {
402
                        QString newName=searchBase+PicTable->text(i, COL_FILENAME);
403
                        if (QFile::exists(newName))
404
                        {
405
                                if (loadPictByRow(newName, i))
406
                                        ++updated;
407
                        }
408
                }
409
        }
410
        if (updated>0)
411
                PicTable->adjustColumn(COL_PREVIEW);
412
}