From 47e1f28f1fe9fecf969d63f059b8ec8c0184605e Mon Sep 17 00:00:00 2001
From: Bent Bisballe Nyeng <deva@aasimon.org>
Date: Fri, 22 Mar 2013 22:42:05 +0100
Subject: Added Engine->Messageing system. Implemented LoadStatus Message.
 Added ProgressBar widget. Made FileBrowser chdir to old value (needs further
 fixing). More printf->hugin ports.

---
 plugingui/Makefile.am.plugingui |  1 +
 plugingui/filebrowser.cc        | 32 +++++++++++++++--
 plugingui/plugingui.cc          | 34 +++++++++++++++++-
 plugingui/plugingui.h           |  3 ++
 plugingui/progressbar.cc        | 77 +++++++++++++++++++++++++++++++++++++++++
 plugingui/progressbar.h         | 54 +++++++++++++++++++++++++++++
 src/audiofile.cc                |  2 +-
 src/drumgizmo.cc                | 23 ++++++++----
 src/drumgizmo.h                 | 13 ++++---
 src/drumkitloader.cc            | 46 ++++++++++++++++++------
 src/drumkitloader.h             |  5 ++-
 src/instrument.cc               |  6 ++--
 12 files changed, 267 insertions(+), 29 deletions(-)
 create mode 100644 plugingui/progressbar.cc
 create mode 100644 plugingui/progressbar.h

diff --git a/plugingui/Makefile.am.plugingui b/plugingui/Makefile.am.plugingui
index c020fc6..c617524 100644
--- a/plugingui/Makefile.am.plugingui
+++ b/plugingui/Makefile.am.plugingui
@@ -24,6 +24,7 @@ PLUGIN_GUI_SOURCES = \
 	$(top_srcdir)/plugingui/filebrowser.cc \
 	$(top_srcdir)/plugingui/image.cc \
 	$(top_srcdir)/plugingui/combobox.cc \
+	$(top_srcdir)/plugingui/progressbar.cc \
 	$(top_srcdir)/plugingui/resource.cc \
 	$(top_srcdir)/plugingui/resource_data.cc 
 
diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc
index afd6518..7efd050 100644
--- a/plugingui/filebrowser.cc
+++ b/plugingui/filebrowser.cc
@@ -38,6 +38,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <hugin.hpp>
+
 #ifdef WIN32
 #include <direct.h>
 #endif
@@ -189,9 +191,33 @@ GUI::FileBrowser::~FileBrowser()
 
 void GUI::FileBrowser::setPath(std::string path)
 {
-  int i = chdir(path.c_str());
-  (void)i;
-  changeDir(prv);
+  std::string dirname = path;
+
+  while(dirname != "") {
+
+    DEBUG(filebrowser, "dirname: %s\n", dirname.c_str());
+
+    struct stat st;
+    if(stat(dirname.c_str(), &st) == 0) {
+      if((st.st_mode & S_IFDIR) != 0) {
+        dirname += "/.";
+        int i = chdir(dirname.c_str());
+        (void)i;
+        changeDir(prv);
+
+        DEBUG(filebrowser, "chdir to: %s\n", dirname.c_str());
+
+        return;
+      }
+    }
+
+    dirname = dirname.substr(0, dirname.length() - 1);
+    while(dirname[dirname.length() - 1] != '/' &&
+          dirname[dirname.length() - 1] != '\\' &&
+          dirname != "") { 
+      dirname = dirname.substr(0, dirname.length() - 1);
+    }
+  }
 }
 
 void GUI::FileBrowser::resize(size_t w, size_t h)
diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc
index 3fb5984..e1de5b5 100644
--- a/plugingui/plugingui.cc
+++ b/plugingui/plugingui.cc
@@ -36,6 +36,7 @@
 #include <drumgizmo.h>
 #include "../src/configuration.h"
 #else
+#include "../src/message.h"
 class DrumGizmo {
 public:
   void loadkit(std::string) {}
@@ -88,6 +89,7 @@ static void kitBrowseClick(void *ptr)
 {
   PluginGUI *gui = (PluginGUI*)ptr;
 
+  fb->setPath(gui->lineedit->text());
   fb->registerFileSelectHandler(selectKitFile, gui);
   fb->show();
 }
@@ -109,6 +111,7 @@ static void midimapBrowseClick(void *ptr)
 {
   PluginGUI *gui = (PluginGUI*)ptr;
 
+  fb->setPath(gui->lineedit2->text());
   fb->registerFileSelectHandler(selectMapFile, gui);
   fb->show();
 }
@@ -168,7 +171,32 @@ void PluginGUI::thread_main()
 #else
     usleep(50);
 #endif/*WIN32*/
-    //    printf("."); fflush(stdout);
+    Message *msg;
+    while((msg = drumgizmo->receiveMessage()) != NULL) {
+      switch(msg->type()) {
+      case Message::LoadStatus:
+        {
+          Message *pmsg;
+          while( (pmsg = drumgizmo->peekMessage()) != NULL) {
+            if(pmsg->type() != Message::LoadStatus) break;
+            delete msg;
+            msg = drumgizmo->receiveMessage();
+          } 
+          LoadStatus *ls = (LoadStatus*)msg;
+          DEBUG(gui, "%d of %d (%s)\n",
+                ls->numer_of_files_loaded,
+                ls->number_of_files,
+                ls->current_file.c_str());
+          progress->setProgress((float)ls->numer_of_files_loaded /
+                                (float)ls->number_of_files);
+        }
+        break;
+      default:
+        break;
+      }
+
+      delete msg;
+    }
   }
 }
 
@@ -274,6 +302,10 @@ void PluginGUI::init()
   lbl3->move(120, 180);
   lbl3->resize(70, 20);
 
+  progress = new GUI::ProgressBar(window);
+  progress->move(200, window->height() - 17);
+  progress->resize(window->width() - 400, 16);
+
   // Create filebrowser
   filebrowser = new GUI::FileBrowser(window);
   filebrowser->move(0, 0);
diff --git a/plugingui/plugingui.h b/plugingui/plugingui.h
index 1085601..d78e480 100644
--- a/plugingui/plugingui.h
+++ b/plugingui/plugingui.h
@@ -36,6 +36,7 @@
 #include "checkbox.h"
 #include "button.h"
 #include "knob.h"
+#include "progressbar.h"
 
 #include "filebrowser.h"
 
@@ -82,6 +83,8 @@ public:
   FileLineEdit *lineedit2;
   GUI::LED *led2;
 
+  GUI::ProgressBar *progress;
+
   void (*windowClosedHandler)(void *);
   void *windowClosedPtr;
 
diff --git a/plugingui/progressbar.cc b/plugingui/progressbar.cc
new file mode 100644
index 0000000..d7048a4
--- /dev/null
+++ b/plugingui/progressbar.cc
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            progressbar.cc
+ *
+ *  Fri Mar 22 22:07:57 CET 2013
+ *  Copyright 2013 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo 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.
+ *
+ *  DrumGizmo is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#include "progressbar.h"
+
+#include "painter.h"
+
+GUI::ProgressBar::ProgressBar(GUI::Widget *parent) : GUI::Widget(parent)
+{
+  _progress = 0;
+}
+
+float GUI::ProgressBar::progress()
+{
+  return _progress;
+}
+
+void GUI::ProgressBar::setProgress(float progress)
+{
+  _progress = progress;
+  repaintEvent(NULL);
+}
+
+void GUI::ProgressBar::repaintEvent(GUI::RepaintEvent *e)
+{
+  Painter p(this);
+
+  int max = width() - 1;
+
+  p.clear();
+
+  p.setColour(Colour(0, 0, 1, 0.3));
+  p.drawFilledRectangle(1, 1, max * _progress - 2, height() - 1);
+
+  p.setColour(Colour(1));
+  p.drawRectangle(0,0,max,height()-1);
+}
+
+#ifdef TEST_PROGRESSBAR
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+// TODO: Put some testcode here (see test.h for usable macros).
+
+TEST_END;
+
+#endif/*TEST_PROGRESSBAR*/
diff --git a/plugingui/progressbar.h b/plugingui/progressbar.h
new file mode 100644
index 0000000..9ab4109
--- /dev/null
+++ b/plugingui/progressbar.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ *            progressbar.h
+ *
+ *  Fri Mar 22 22:07:57 CET 2013
+ *  Copyright 2013 Bent Bisballe Nyeng
+ *  deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ *  This file is part of DrumGizmo.
+ *
+ *  DrumGizmo 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.
+ *
+ *  DrumGizmo is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with DrumGizmo; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+#ifndef __DRUMGIZMO_PROGRESSBAR_H__
+#define __DRUMGIZMO_PROGRESSBAR_H__
+
+#include "widget.h"
+
+#include <string>
+
+#include "guievent.h"
+
+namespace GUI {
+
+class ProgressBar : public Widget {
+public:
+  ProgressBar(Widget *parent);
+
+  float progress();
+  void setProgress(float progress);
+
+  //protected:
+  virtual void repaintEvent(RepaintEvent *e);
+
+private:
+  float _progress;
+};
+
+};
+
+#endif/*__DRUMGIZMO_PROGRESSBAR_H__*/
diff --git a/src/audiofile.cc b/src/audiofile.cc
index 1927034..3f0bd82 100644
--- a/src/audiofile.cc
+++ b/src/audiofile.cc
@@ -82,7 +82,7 @@ void AudioFile::load()
   is_loaded = true;
   mutex.unlock();
 
-  DEBUG(audiofile, "Loading of %s completed.\n", filename.c_str());
+  //DEBUG(audiofile, "Loading of %s completed.\n", filename.c_str());
 }
 
 bool AudioFile::isLoaded()
diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc
index 93426de..e8ced6a 100644
--- a/src/drumgizmo.cc
+++ b/src/drumgizmo.cc
@@ -39,7 +39,7 @@
 #include "drumkitparser.h"
 
 DrumGizmo::DrumGizmo(AudioOutputEngine *o, AudioInputEngine *i)
-  : oe(o), ie(i)
+  : loader(this), oe(o), ie(i)
 {
   loader.run(); // Start drumkit loader thread.
 }
@@ -57,13 +57,12 @@ DrumGizmo::~DrumGizmo()
 }
 
 /*
- * Send a message to the engine. The engine takes over the memory.
+ * Add a message to the GUI message queue.
  */
 void DrumGizmo::sendMessage(Message *msg)
 {
-  message_mutex.lock();
+  MutexAutolock l(message_mutex);
   message_queue.push_back(msg);
-  message_mutex.unlock();
 }
 
 /*
@@ -71,13 +70,25 @@ void DrumGizmo::sendMessage(Message *msg)
  */
 Message *DrumGizmo::receiveMessage()
 {
+  MutexAutolock l(message_mutex);
   Message *msg = NULL;
-  message_mutex.lock();
   if(message_queue.size()) {
     msg = message_queue.front();
     message_queue.pop_front();
   }
-  message_mutex.unlock();
+  return msg;
+}
+
+/*
+ * Receive message from the engine without removing it from the queue.
+ */
+Message *DrumGizmo::peekMessage()
+{
+  MutexAutolock l(message_mutex);
+  Message *msg = NULL;
+  if(message_queue.size()) {
+    msg = message_queue.front();
+  }
   return msg;
 }
 
diff --git a/src/drumgizmo.h b/src/drumgizmo.h
index 92faa3c..b7df7b9 100644
--- a/src/drumgizmo.h
+++ b/src/drumgizmo.h
@@ -71,14 +71,19 @@ public:
   std::string kitfile;
 
   /*
-   * Send a message to the engine. The engine takes over the memory.
+   * Receive message from the engine. The caller takes over the memory.
    */
-  void sendMessage(Message *msg);
+  Message *receiveMessage();
 
   /*
-   * Receive message from the engine. The caller takes over the memory.
+   * Receive message from the engine without removing it from the queue.
    */
-  Message *receiveMessage();
+  Message *peekMessage();
+
+  /*
+   * Add a message to the GUI message queue.
+   */
+  void sendMessage(Message *msg);
 
 private:
   Mutex message_mutex;
diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc
index 9f514d0..53af7db 100644
--- a/src/drumkitloader.cc
+++ b/src/drumkitloader.cc
@@ -29,9 +29,11 @@
 #include <hugin.hpp>
 
 #include "drumkitparser.h"
+#include "drumgizmo.h"
 
-DrumKitLoader::DrumKitLoader()
+DrumKitLoader::DrumKitLoader(DrumGizmo *dg)
 {
+  drumgizmo = dg;
   is_done = false;
   quitit = false;
 }
@@ -75,19 +77,41 @@ void DrumKitLoader::thread_main()
 
     if(quitit) return;
 
-    Instruments::iterator i = kit->instruments.begin();
-    while(i != kit->instruments.end()) {
-      Instrument *instr = *i;
+    unsigned int count = 0;
+    { // Count total number of files that need loading:
+      Instruments::iterator i = kit->instruments.begin();
+      while(i != kit->instruments.end()) {
+        Instrument *instr = *i;
 
-      std::vector<AudioFile*>::iterator a = instr->audiofiles.begin();
-      while(a != instr->audiofiles.end()) {
-        //        usleep(10000);
-        AudioFile *af = *a;
-        af->load();
-        a++;
+        count += instr->audiofiles.size();
+        i++;
       }
+    }
 
-      i++;
+    { // Now actually load them:
+      unsigned int loaded = 0;
+      Instruments::iterator i = kit->instruments.begin();
+      while(i != kit->instruments.end()) {
+        Instrument *instr = *i;
+        
+        std::vector<AudioFile*>::iterator a = instr->audiofiles.begin();
+        while(a != instr->audiofiles.end()) {
+          //usleep(5000);
+          AudioFile *af = *a;
+          af->load();
+          loaded++;
+
+          LoadStatus *ls = new LoadStatus();
+          ls->number_of_files = count;
+          ls->numer_of_files_loaded = loaded;
+          ls->current_file = af->filename;
+          drumgizmo->sendMessage(ls);
+          
+          a++;
+        }
+        
+        i++;
+      }
     }
 
     mutex.lock();
diff --git a/src/drumkitloader.h b/src/drumkitloader.h
index c308761..b9bc102 100644
--- a/src/drumkitloader.h
+++ b/src/drumkitloader.h
@@ -35,9 +35,11 @@
 
 #include "drumkit.h"
 
+class DrumGizmo;
+
 class DrumKitLoader : public Thread {
 public:
-  DrumKitLoader();
+  DrumKitLoader(DrumGizmo *drumgizmo);
   ~DrumKitLoader();
 
   void loadKit(DrumKit *kit);
@@ -47,6 +49,7 @@ public:
   bool isDone();
 
 private:
+  DrumGizmo *drumgizmo;
   Semaphore semaphore;
   DrumKit *kit;
   bool is_done;
diff --git a/src/instrument.cc b/src/instrument.cc
index b10d990..297e6c6 100644
--- a/src/instrument.cc
+++ b/src/instrument.cc
@@ -29,19 +29,21 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include <hugin.hpp>
+
 #include "sample.h"
 #include "configuration.h"
 
 Instrument::Instrument()
 {
-  printf("new Instrument %p\n", this);
+  DEBUG(instrument, "new %p\n", this);
   mod = 1.0;
   lastpos = 0;
 }
 
 Instrument::~Instrument()
 {
-  printf("delete Instrument %p\n", this);
+  DEBUG(instrument, "delete %p\n", this);
   std::vector<AudioFile*>::iterator i = audiofiles.begin();
   while(i != audiofiles.end()) {
     delete *i;
-- 
cgit v1.2.3