Embedding: bring embedding dialog back to shape

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20129 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Bo Peng 2007-09-07 16:33:55 +00:00
parent 92065820e5
commit e9a2729037
7 changed files with 173 additions and 73 deletions

View File

@ -139,7 +139,7 @@ bool EmbeddedFile::extract(Buffer const * buf) const
} }
bool EmbeddedFile::embed(Buffer const * buf) bool EmbeddedFile::update(Buffer const * buf) const
{ {
string ext_file = absFilename(); string ext_file = absFilename();
string emb_file = embeddedFile(buf); string emb_file = embeddedFile(buf);
@ -161,17 +161,16 @@ bool EmbeddedFile::embed(Buffer const * buf)
if (!fs::is_directory(path)) if (!fs::is_directory(path))
makedir(const_cast<char*>(path.c_str()), 0755); makedir(const_cast<char*>(path.c_str()), 0755);
fs::copy_file(ext_file, emb_file, false); fs::copy_file(ext_file, emb_file, false);
embedded_ = true;
} catch (fs::filesystem_error const & fe) { } catch (fs::filesystem_error const & fe) {
Alert::error(_("Copy file failure"), Alert::error(_("Copy file failure"),
bformat(_("Cannot copy file %1$s to %2$s.\n" bformat(_("Cannot copy file %1$s to %2$s.\n"
"Please check whether the directory exists and is writeable."), "Please check whether the directory exists and is writeable."),
from_utf8(ext_file), from_utf8(emb_file))); from_utf8(ext_file), from_utf8(emb_file)));
LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
embedded_ = false; return false;
} }
} }
return embedded_; return true;
} }
@ -207,7 +206,8 @@ void EmbeddedFiles::registerFile(string const & filename,
// find this filename // find this filename
if (it != file_list_.end()) { if (it != file_list_.end()) {
it->setParIter(pit); it->setParIter(pit);
it->embed(buffer_); it->update(buffer_);
it->setEmbed(true);
it->validate(); it->validate();
return; return;
} }

View File

@ -127,6 +127,9 @@ public:
/// embedding status of this file /// embedding status of this file
bool embedded() const { return embedded_; } bool embedded() const { return embedded_; }
/// set embedding status. update() should be called before this
/// to sync the embedded file with external one.
bool setEmbed(bool embed) { embedded_ = embed; }
// A flag indicating whether or not this filename is valid. // A flag indicating whether or not this filename is valid.
// When lyx runs, InsetGraphics etc may be added or removed so filename // When lyx runs, InsetGraphics etc may be added or removed so filename
@ -137,10 +140,11 @@ public:
bool valid() const { return valid_; } bool valid() const { return valid_; }
void validate() { valid_ = true; } void validate() { valid_ = true; }
void invalidate() { valid_ = false; } void invalidate() { valid_ = false; }
///
/// extract file, does not change embedding status
bool extract(Buffer const * buf) const; bool extract(Buffer const * buf) const;
/// /// update embedded file from external file, does not change embedding status
bool embed(Buffer const * buf); bool update(Buffer const * buf) const;
private: private:
/// filename in zip file /// filename in zip file

View File

@ -59,10 +59,11 @@ void ControlEmbeddedFiles::updateEmbeddedFiles()
} }
void ControlEmbeddedFiles::dispatchParams() void ControlEmbeddedFiles::dispatchMessage(string const & msg)
{ {
// FIXME: the right thing to do? QT guys?
// lyx view will only be updated if we do something to the main window. :-) // lyx view will only be updated if we do something to the main window. :-)
kernel().dispatch(FuncRequest(LFUN_MESSAGE, message_)); kernel().dispatch(FuncRequest(LFUN_MESSAGE, msg));
} }
@ -82,6 +83,16 @@ void ControlEmbeddedFiles::view(EmbeddedFile const & item)
} }
void ControlEmbeddedFiles::setEmbed(EmbeddedFile & item, bool embed)
{
if (embed)
item.update(&kernel().buffer());
else
item.extract(&kernel().buffer());
item.setEmbed(embed);
}
docstring const ControlEmbeddedFiles::browseFile() docstring const ControlEmbeddedFiles::browseFile()
{ {
std::pair<docstring, docstring> dir1(_("Documents|#o#O"), std::pair<docstring, docstring> dir1(_("Documents|#o#O"),
@ -99,5 +110,10 @@ bool ControlEmbeddedFiles::extract(EmbeddedFile const & item)
} }
bool ControlEmbeddedFiles::update(EmbeddedFile const & item)
{
return item.update(&kernel().buffer());
}
} // namespace frontend } // namespace frontend
} // namespace lyx } // namespace lyx

View File

@ -40,17 +40,21 @@ public:
/// ///
virtual bool canApplyToReadOnly() const { return true; } virtual bool canApplyToReadOnly() const { return true; }
/// ///
void setMessage(std::string const & msg) { message_ = msg; } void dispatchMessage(std::string const & msg);
/// ///
void dispatchParams(); void dispatchParams() {};
/// ///
void goTo(EmbeddedFile const & item); void goTo(EmbeddedFile const & item);
/// ///
void view(EmbeddedFile const & item); void view(EmbeddedFile const & item);
/// ///
void setEmbed(EmbeddedFile & item, bool embed);
///
docstring const browseFile(); docstring const browseFile();
/// ///
bool extract(EmbeddedFile const & item); bool extract(EmbeddedFile const & item);
///
bool update(EmbeddedFile const & item);
protected: protected:
// //

View File

@ -17,10 +17,6 @@
namespace lyx { namespace lyx {
namespace frontend { namespace frontend {
static QString const INVALID_COLOR = "gray";
static QString const EMBEDDED_COLOR = "black";
static QString const EXTERNAL_COLOR = "blue";
GuiEmbeddedFilesDialog::GuiEmbeddedFilesDialog(LyXView & lv) GuiEmbeddedFilesDialog::GuiEmbeddedFilesDialog(LyXView & lv)
: GuiDialog(lv, "embedding") : GuiDialog(lv, "embedding")
@ -44,34 +40,56 @@ ControlEmbeddedFiles & GuiEmbeddedFilesDialog::controller() const
} }
void GuiEmbeddedFilesDialog::on_filesLW_itemChanged(QListWidgetItem* item)
{
EmbeddedFiles & files = controller().embeddedFiles();
if (item->checkState() == Qt::Checked) {
controller().setEmbed(files[filesLW->row(item)], true);
controller().dispatchMessage("Embed file " + fromqstr(item->text()));
} else {
controller().setEmbed(files[filesLW->row(item)], false);
controller().dispatchMessage("Stop embedding file " + fromqstr(item->text()));
}
}
void GuiEmbeddedFilesDialog::on_filesLW_itemSelectionChanged() void GuiEmbeddedFilesDialog::on_filesLW_itemSelectionChanged()
{ {
EmbeddedFiles & files = controller().embeddedFiles(); EmbeddedFiles & files = controller().embeddedFiles();
QList<QListWidgetItem *> selection = filesLW->selectedItems(); QList<QListWidgetItem *> selection = filesLW->selectedItems();
if (selection.empty()) {
fullpathLE->setEnabled(false);
selectPB->setEnabled(false);
unselectPB->setEnabled(false);
extractPB->setEnabled(false);
updatePB->setEnabled(false);
return;
}
fullpathLE->setEnabled(selection.size() == 1); fullpathLE->setEnabled(selection.size() == 1);
// try to find a common mode, otherwise return NONE. // try to find a common embedding status
QList<QListWidgetItem*>::iterator it = selection.begin(); QList<QListWidgetItem*>::iterator it = selection.begin();
QList<QListWidgetItem*>::iterator it_end = selection.end(); QList<QListWidgetItem*>::iterator it_end = selection.end();
// if the selection is not empty // if the selection is not empty
if (it != it_end) { if (it != it_end) {
int idx = filesLW->row(*it); int idx = filesLW->row(*it);
fullpathLE->setText(toqstr(files[idx].absFilename())); fullpathLE->setText(toqstr(files[idx].absFilename()));
// go to the first selected item
controller().goTo(files[idx]); controller().goTo(files[idx]);
} }
// are the items all selected or unselected?
bool mode = false; bool hasSelected = false;
bool hasUnselected = false;
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
int idx = filesLW->row(*it); if ((*it)->checkState() == Qt::Checked)
if (mode) { hasSelected = true;
mode = true; else
continue; hasUnselected = true;
} }
} selectPB->setEnabled(hasUnselected);
unselectPB->setEnabled(hasSelected);
} }
@ -86,25 +104,48 @@ void GuiEmbeddedFilesDialog::on_filesLW_itemDoubleClicked()
void GuiEmbeddedFilesDialog::updateView() void GuiEmbeddedFilesDialog::updateView()
{ {
filesLW->clear(); filesLW->clear();
// //
EmbeddedFiles const & files = controller().embeddedFiles(); EmbeddedFiles const & files = controller().embeddedFiles();
EmbeddedFiles::EmbeddedFileList::const_iterator it = files.begin(); EmbeddedFiles::EmbeddedFileList::const_iterator it = files.begin();
EmbeddedFiles::EmbeddedFileList::const_iterator it_end = files.end(); EmbeddedFiles::EmbeddedFileList::const_iterator it_end = files.end();
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
QListWidgetItem * item = new QListWidgetItem(toqstr(it->inzipName())); QListWidgetItem * item = new QListWidgetItem(toqstr(it->inzipName()));
if (!it->valid()) Qt::ItemFlags flag = Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
item->setTextColor(INVALID_COLOR); if (it->valid())
else if(it->embedded()) flag |= Qt::ItemIsEnabled;
item->setTextColor(EMBEDDED_COLOR); item->setFlags(flag);
if(it->embedded())
item->setCheckState(Qt::Checked);
else else
item->setTextColor(EXTERNAL_COLOR); item->setCheckState(Qt::Unchecked);
filesLW->addItem(item); filesLW->addItem(item);
} }
// }
bool enabled = files.enabled();
enableCB->setChecked(enabled);
fullpathLE->setEnabled(enabled); void GuiEmbeddedFilesDialog::on_selectPB_clicked()
{
EmbeddedFiles & files = controller().embeddedFiles();
QList<QListWidgetItem *> selection = filesLW->selectedItems();
for (QList<QListWidgetItem*>::iterator it = selection.begin();
it != selection.end(); ++it) {
(*it)->setCheckState(Qt::Checked);
controller().setEmbed(files[filesLW->row(*it)], true);
}
controller().dispatchMessage("Embedding files");
}
void GuiEmbeddedFilesDialog::on_unselectPB_clicked()
{
EmbeddedFiles & files = controller().embeddedFiles();
QList<QListWidgetItem *> selection = filesLW->selectedItems();
for (QList<QListWidgetItem*>::iterator it = selection.begin();
it != selection.end(); ++it) {
(*it)->setCheckState(Qt::Checked);
controller().setEmbed(files[filesLW->row(*it)], false);
}
controller().dispatchMessage("Stop embedding files");
} }
@ -115,6 +156,7 @@ void GuiEmbeddedFilesDialog::on_addPB_clicked()
EmbeddedFiles & files = controller().embeddedFiles(); EmbeddedFiles & files = controller().embeddedFiles();
files.registerFile(to_utf8(file), true); files.registerFile(to_utf8(file), true);
} }
controller().dispatchMessage("Add an embedded file");
} }
@ -125,46 +167,33 @@ void GuiEmbeddedFilesDialog::on_extractPB_clicked()
for (QList<QListWidgetItem*>::iterator it = selection.begin(); for (QList<QListWidgetItem*>::iterator it = selection.begin();
it != selection.end(); ++it) it != selection.end(); ++it)
controller().extract(files[filesLW->row(*it)]); controller().extract(files[filesLW->row(*it)]);
// FIXME: collect extraction status and display a dialog
controller().dispatchMessage("Extract embedded files");
} }
void GuiEmbeddedFilesDialog::on_updatePB_clicked()
{
EmbeddedFiles const & files = controller().embeddedFiles();
QList<QListWidgetItem *> selection = filesLW->selectedItems();
for (QList<QListWidgetItem*>::iterator it = selection.begin();
it != selection.end(); ++it)
controller().update(files[filesLW->row(*it)]);
// FIXME: collect update status and display a dialog
controller().dispatchMessage("Update embedded files from external file");
}
void GuiEmbeddedFilesDialog::on_enableCB_toggled(bool enable) void GuiEmbeddedFilesDialog::on_enableCB_toggled(bool enable)
{ {
// FIXME:
//
// When a embedded file is turned to disabled, it should save its
// embedded files. Otherwise, embedded files will be lost!!!
// //
controller().embeddedFiles().enable(enable); controller().embeddedFiles().enable(enable);
// immediately post the change to buffer (and bufferView) // immediately post the change to buffer (and bufferView)
if (enable) if (enable)
controller().setMessage("Enable file embedding"); controller().dispatchMessage("Enable file embedding");
else else
controller().setMessage("Disable file embedding"); controller().dispatchMessage("Disable file embedding");
// update bufferView
controller().dispatchParams();
}
void GuiEmbeddedFilesDialog::set_embedding_status(bool embed)
{
EmbeddedFiles & files = controller().embeddedFiles();
QList<QListWidgetItem *> selection = filesLW->selectedItems();
for (QList<QListWidgetItem*>::iterator it = selection.begin();
it != selection.end(); ++it) {
int row = filesLW->row(*it);
// FIXME: mark buffer dirty
if(embed)
(*it)->setTextColor(EMBEDDED_COLOR);
else
(*it)->setTextColor(EXTERNAL_COLOR);
}
if (embed)
controller().setMessage("Embed file");
else
controller().setMessage("Extract file");
// update bufferView
controller().dispatchParams();
} }

View File

@ -29,17 +29,19 @@ public:
public Q_SLOTS: public Q_SLOTS:
/// ///
void on_filesLW_itemChanged(QListWidgetItem* item);
void on_filesLW_itemSelectionChanged(); void on_filesLW_itemSelectionChanged();
///
void on_filesLW_itemDoubleClicked(); void on_filesLW_itemDoubleClicked();
/// ///
void updateView(); void updateView();
/// ///
void on_enableCB_toggled(bool enable); void on_enableCB_toggled(bool enable);
/// ///
void on_selectPB_clicked();
void on_unselectPB_clicked();
void on_addPB_clicked(); void on_addPB_clicked();
//
void on_extractPB_clicked(); void on_extractPB_clicked();
void on_updatePB_clicked();
private: private:
ControlEmbeddedFiles & controller() const; ControlEmbeddedFiles & controller() const;

View File

@ -8,7 +8,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>145</width> <width>199</width>
<height>390</height> <height>390</height>
</rect> </rect>
</property> </property>
@ -25,7 +25,14 @@
<item> <item>
<widget class="QCheckBox" name="enableCB" > <widget class="QCheckBox" name="enableCB" >
<property name="text" > <property name="text" >
<string>Enable embedding</string> <string>Save file in bundled format</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label" >
<property name="text" >
<string>Embedding status</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -60,6 +67,44 @@
<property name="spacing" > <property name="spacing" >
<number>6</number> <number>6</number>
</property> </property>
<item>
<widget class="QToolButton" name="selectPB" >
<property name="toolTip" >
<string>Embed selected files</string>
</property>
<property name="text" >
<string>...</string>
</property>
<property name="icon" >
<iconset>../../../../lib/images/tabular-feature_set-all-lines.png</iconset>
</property>
<property name="iconSize" >
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="unselectPB" >
<property name="toolTip" >
<string>Do not embed selected files</string>
</property>
<property name="text" >
<string>...</string>
</property>
<property name="icon" >
<iconset>../../../../lib/images/tabular-feature_set-all-lines.png</iconset>
</property>
<property name="iconSize" >
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</widget>
</item>
<item> <item>
<widget class="QToolButton" name="addPB" > <widget class="QToolButton" name="addPB" >
<property name="toolTip" > <property name="toolTip" >
@ -82,7 +127,7 @@
<item> <item>
<widget class="QToolButton" name="extractPB" > <widget class="QToolButton" name="extractPB" >
<property name="toolTip" > <property name="toolTip" >
<string>Extract selected file.</string> <string>Extract selected file, without changing embedding status.</string>
</property> </property>
<property name="text" > <property name="text" >
<string>...</string> <string>...</string>
@ -107,7 +152,7 @@
<string>...</string> <string>...</string>
</property> </property>
<property name="icon" > <property name="icon" >
<iconset>../../../../lib/images/copy.png</iconset> <iconset>../../../../lib/images/depth-decrement.png</iconset>
</property> </property>
<property name="iconSize" > <property name="iconSize" >
<size> <size>