OpenAction.cpp 3.67 KB
Newer Older
1
2
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
3
 *
4
 * CamiTK - Computer Assisted Medical Intervention ToolKit
promayon's avatar
promayon committed
5
 * (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
6
 *
7
 * Visit http://camitk.imag.fr for more information
8
 *
9
10
11
12
13
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
14
 *
15
 * CamiTK is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
19
 *
20
21
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 * $CAMITK_LICENCE_END$
24
 ****************************************************************************/
25
26
27
28
#include "OpenAction.h"

// CamiTK
#include <Application.h>
29
30
31
32
33
#include <ExtensionManager.h>

// Qt
#include <QFileDialog>
#include <fcntl.h>
34
35
36
37

using namespace camitk;

// --------------- constructor -------------------
38
39
40
41
42
43
OpenAction::OpenAction (ActionExtension * extension) : Action (extension) {
    // Setting name, description and input component
    setName ("Open");
    setEmbedded (false);
    setDescription ("Open data (component) from a file");
    setComponent ("");
promayon's avatar
promayon committed
44
45
    if (qApp->type() != QApplication::Tty)
      setIcon (QPixmap (":/fileOpen"));
46
47
48
49

    // Setting classification family and tags
    setFamily ("Application");
    addTag ("Open");
50
    
51
    // add a shortcut
promayon's avatar
promayon committed
52
53
54
55
    if (qApp->type() != QApplication::Tty) {
      getQAction()->setShortcut( QKeySequence::Open );
      getQAction()->setShortcutContext(Qt::ApplicationShortcut);
    }
56
57
58
59
60
61
62
63
64
65
66
67
}

// --------------- destructor -------------------
OpenAction::~OpenAction() {
}

// --------------- getWidget --------------
QWidget * OpenAction::getWidget() {
    return NULL;
}

// --------------- apply -------------------
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Action::ApplyStatus OpenAction::apply() {
    Application::showStatusBarMessage(tr("Opening file..."));

    // filter: extension of known file format
    QString filter;
    QString allKnownFiles; //example: (*.xml *.vtk *.wrl)

    // first item = all known files
    filter += QString ("All known files (*.") + ExtensionManager::getFileExtensions().join(" *.") + ");;";

    // add the extension of loaded and valid plugins
    foreach(ComponentExtension * ext, ExtensionManager::getComponentExtensions().values().toSet()) {
	filter += ext->getName() + " (*." + ext->getFileExtensions().join(" *.") + ");;";
    }

    // Open more than one file
    QStringList files = QFileDialog::getOpenFileNames (NULL, "Select One or More Files to Open", Application::getLastUsedDirectory().absolutePath(), filter);

86
    if (!files.isEmpty()) {        
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	QStringList listOfFiles = files; // as stated by the Qt doc (although without further information): "If you want to iterate over the list of files, you should iterate over a copy."
	QStringList::const_iterator fileIterator = listOfFiles.begin();
	while (fileIterator != listOfFiles.constEnd() && Application::open(*fileIterator) !=NULL)
            ++fileIterator;
	if (fileIterator == listOfFiles.constEnd()) {
            Application::showStatusBarMessage(tr ("All files loaded."));
            return SUCCESS;
        }
        else {
            Application::showStatusBarMessage(tr ("Error loading files: ") + (*fileIterator));
            return ERROR;
        }
    }
    else {
        Application::showStatusBarMessage(tr ("Open aborted."));
        return ABORTED;
    }
104
105
}

106