]> git.lyx.org Git - lyx.git/commitdiff
Embedding: bring embedding dialog back to shape
authorBo Peng <bpeng@lyx.org>
Fri, 7 Sep 2007 16:33:55 +0000 (16:33 +0000)
committerBo Peng <bpeng@lyx.org>
Fri, 7 Sep 2007 16:33:55 +0000 (16:33 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20129 a592a061-630c-0410-9148-cb99ea01b6c8

src/EmbeddedFiles.cpp
src/EmbeddedFiles.h
src/frontends/controllers/ControlEmbeddedFiles.cpp
src/frontends/controllers/ControlEmbeddedFiles.h
src/frontends/qt4/GuiEmbeddedFiles.cpp
src/frontends/qt4/GuiEmbeddedFiles.h
src/frontends/qt4/ui/EmbeddedFilesUi.ui

index 83531f131f628fff6c02d93c7efbe534e38481d8..e67d1511413e968bc56f925ffa7cf419efa27766 100644 (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 emb_file = embeddedFile(buf);
@@ -161,17 +161,16 @@ bool EmbeddedFile::embed(Buffer const * buf)
                        if (!fs::is_directory(path))
                                makedir(const_cast<char*>(path.c_str()), 0755);
                        fs::copy_file(ext_file, emb_file, false);
-                       embedded_ = true;
                } catch (fs::filesystem_error const & fe) {
                        Alert::error(_("Copy file failure"),
                                 bformat(_("Cannot copy file %1$s to %2$s.\n"
                                           "Please check whether the directory exists and is writeable."),
                                                from_utf8(ext_file), from_utf8(emb_file)));
                        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
        if (it != file_list_.end()) {
                it->setParIter(pit);
-               it->embed(buffer_);
+               it->update(buffer_);
+               it->setEmbed(true);
                it->validate();
                return;
        }
index 9c63c2d8d5d47194e65144bc6654e3e1fd2e2779..de050c5622539c30d8fe5fbcea207f16a592643e 100644 (file)
@@ -127,6 +127,9 @@ public:
 
        /// embedding status of this file
        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.
        // When lyx runs, InsetGraphics etc may be added or removed so filename
@@ -137,10 +140,11 @@ public:
        bool valid() const { return valid_; }
        void validate() { valid_ = true; }
        void invalidate() {     valid_ = false; }
-       ///
+
+       /// extract file, does not change embedding status
        bool extract(Buffer const * buf) const;
-       ///
-       bool embed(Buffer const * buf);
+       /// update embedded file from external file, does not change embedding status
+       bool update(Buffer const * buf) const;
 
 private:
        /// filename in zip file
index 674568020bdda8ca3b044f3b6a999173822b7ca4..1ea646cf2a51dbe2650f76b3f02bbf3a1a4be5ae 100644 (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. :-)
-       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()
 {
        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 lyx
index e50c98d007eaa3a917fd8bf28dccae9aebeba329..426f42e82d411dd8a588c0a6754359a6158b2f11 100644 (file)
@@ -40,17 +40,21 @@ public:
        ///
        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 view(EmbeddedFile const & item);
        ///
+       void setEmbed(EmbeddedFile & item, bool embed);
+       ///
        docstring const browseFile();
        ///
        bool extract(EmbeddedFile const & item);
+       ///
+       bool update(EmbeddedFile const & item);
 
 protected:
        //
index 0698e7da8ba02ee16ae10a6707f16b3e424116fd..886ecde1ce62b26e1c7da4a0634e8b7c9ac6a414 100644 (file)
 namespace lyx {
 namespace frontend {
 
-static QString const INVALID_COLOR = "gray";
-static QString const EMBEDDED_COLOR = "black";
-static QString const EXTERNAL_COLOR = "blue";
-
 
 GuiEmbeddedFilesDialog::GuiEmbeddedFilesDialog(LyXView & lv)
        : 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()
 {
        EmbeddedFiles & files = controller().embeddedFiles();
 
        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);
 
-       // 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_end = selection.end(); 
        // if the selection is not empty
        if (it != it_end) {
                int idx = filesLW->row(*it);
                fullpathLE->setText(toqstr(files[idx].absFilename()));
-               // go to the first selected item
                controller().goTo(files[idx]);
        }
-
-       bool mode = false;
+       // are the items all selected or unselected?
+       bool hasSelected = false;
+       bool hasUnselected = false;
        for (; it != it_end; ++it) {
-               int idx = filesLW->row(*it);
-               if (mode) {
-                       mode = true;
-                       continue;
-               }
+               if ((*it)->checkState() == Qt::Checked)
+                       hasSelected = true;
+               else
+                       hasUnselected = true;
        }
-                       
+       selectPB->setEnabled(hasUnselected);
+       unselectPB->setEnabled(hasSelected);
 }
 
 
@@ -86,25 +104,48 @@ void GuiEmbeddedFilesDialog::on_filesLW_itemDoubleClicked()
 void GuiEmbeddedFilesDialog::updateView()
 {
        filesLW->clear();
-       
        //
        EmbeddedFiles const & files = controller().embeddedFiles();
        EmbeddedFiles::EmbeddedFileList::const_iterator it = files.begin();
        EmbeddedFiles::EmbeddedFileList::const_iterator it_end = files.end();
        for (; it != it_end; ++it) {
                QListWidgetItem * item = new QListWidgetItem(toqstr(it->inzipName()));
-               if (!it->valid())
-                       item->setTextColor(INVALID_COLOR);
-               else if(it->embedded())
-                       item->setTextColor(EMBEDDED_COLOR);
+               Qt::ItemFlags flag = Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
+               if (it->valid())
+                       flag |= Qt::ItemIsEnabled;
+               item->setFlags(flag);
+               if(it->embedded())
+                       item->setCheckState(Qt::Checked);
                else
-                       item->setTextColor(EXTERNAL_COLOR);
+                       item->setCheckState(Qt::Unchecked);
                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");
 }
 
 
@@ -114,7 +155,8 @@ void GuiEmbeddedFilesDialog::on_addPB_clicked()
        if (!file.empty()) {
                EmbeddedFiles & files = controller().embeddedFiles();
                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(); 
                it != selection.end(); ++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)
 {
-       // 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);
        // immediately post the change to buffer (and bufferView)
        if (enable)
-               controller().setMessage("Enable file embedding");
-       else
-               controller().setMessage("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");
+               controller().dispatchMessage("Enable file embedding");
        else
-               controller().setMessage("Extract file");
-       // update bufferView
-       controller().dispatchParams();
+               controller().dispatchMessage("Disable file embedding");
 }
 
 
index 3d98d8f4857f3cf4d786583d27b1553988332002..44cc425082950509c231c2808b5315bbfb6e707c 100644 (file)
@@ -29,17 +29,19 @@ public:
 
 public Q_SLOTS:
        ///
+       void on_filesLW_itemChanged(QListWidgetItem* item);
        void on_filesLW_itemSelectionChanged();
-       ///
        void on_filesLW_itemDoubleClicked();
        ///
        void updateView();
        ///
        void on_enableCB_toggled(bool enable);
        ///
+       void on_selectPB_clicked();
+       void on_unselectPB_clicked();
        void on_addPB_clicked();
-       //
        void on_extractPB_clicked();
+       void on_updatePB_clicked();
 
 private:
        ControlEmbeddedFiles & controller() const;
index bcf320acc858f9b841f04a5db67b695205d325ff..4eb5e0d84e549f91d5f4be48481694012e40057b 100644 (file)
@@ -8,7 +8,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>145</width>
+    <width>199</width>
     <height>390</height>
    </rect>
   </property>
    <item>
     <widget class="QCheckBox" name="enableCB" >
      <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>
     </widget>
    </item>
      <property name="spacing" >
       <number>6</number>
      </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>
       <widget class="QToolButton" name="addPB" >
        <property name="toolTip" >
      <item>
       <widget class="QToolButton" name="extractPB" >
        <property name="toolTip" >
-        <string>Extract selected file.</string>
+        <string>Extract selected file, without changing embedding status.</string>
        </property>
        <property name="text" >
         <string>...</string>
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/copy.png</iconset>
+        <iconset>../../../../lib/images/depth-decrement.png</iconset>
        </property>
        <property name="iconSize" >
         <size>